Skip to content

Add some docs to stblib Symbol #7336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/artifacts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,9 @@ lib/ocaml/Stdlib_String.resi
lib/ocaml/Stdlib_Symbol.cmi
lib/ocaml/Stdlib_Symbol.cmj
lib/ocaml/Stdlib_Symbol.cmt
lib/ocaml/Stdlib_Symbol.cmti
lib/ocaml/Stdlib_Symbol.res
lib/ocaml/Stdlib_Symbol.resi
lib/ocaml/Stdlib_Type.cmi
lib/ocaml/Stdlib_Type.cmj
lib/ocaml/Stdlib_Type.cmt
Expand Down
5 changes: 4 additions & 1 deletion runtime/Stdlib_Symbol.res
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
type t

@val external make: string => t = "Symbol"
@val external getFor: string => t = "Symbol.for"
@val external getFor: string => option<t> = "Symbol.for"
@val external keyFor: t => option<string> = "Symbol.keyFor"
@get
external description: t => option<string> = "description"
@send external toString: t => string = "toString"

@val external asyncIterator: t = "Symbol.asyncIterator"
@val external hasInstance: t = "Symbol.hasInstance"
Expand Down
105 changes: 105 additions & 0 deletions runtime/Stdlib_Symbol.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/***
A built-in object that serves as a namespace for globally-unique identifiers.

Compiles to a regular JavaScript Symbol.
*/

/**
Type representing a Symbol.
*/
type t

/**
`make(key)`

Makes a new unique Symbol value.

## Examples

```rescript
Symbol.make("sym1")->assetEqual("sym1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad it's assertEqual

Suggested change
Symbol.make("sym1")->assetEqual("sym1")
Symbol.make("sym1")->assertEqual("sym1")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

```
*/
@val
external make: string => t = "Symbol"

/**
`getFor(key)`

Searches for existing registered Symbols in the global Symbol registry with the given key and returns it if found.
Otherwise a new Symbol gets created and registered with key.

## Examples

```rescript
Symbol.getFor("sym1")->assetEqual(Symbol.getFor("sym1"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

```
*/
@val
external getFor: string => t = "Symbol.for"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also forgot to update the interface file, and while we're there, I'd also use @scope for these bindings, which is cleaner IMO:

Suggested change
external getFor: string => t = "Symbol.for"
@scope("Symbol")
external getFor: string => option<t> = "for"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


/**
`keyFor(key)`

Retrieves a shared Symbol key from the global Symbol registry for the given Symbol.

## Examples

```rescript
let globalSym = Symbol.getFor("sym1") // Global symbol

Symbol.keyFor(globalSym)->assertEqual(Some("sym1"))

let localSym = Symbol.make("sym2") // Local symbol

Symbol.keyFor(localSym)->assertEqual(None)
```
*/
@val
external keyFor: t => option<string> = "Symbol.keyFor"

/**
`description`

Returns `Some(string)` containing the description of this symbol, or `None` if the symbol has no description.
## Examples

```rescript
let sym = Symbol.make("sym1")
Symbol.description(sym)->assertEqual(Some("sym1"))
```
*/
@get
external description: t => option<string> = "description"

/**
`toString`

// Returns a string representing this symbol value.

## Examples

```rescript
let sym = Symbol.make("sym1")

Symbol.toString(sym)->assertEqual("Symbol(sym1)")
```
*/
@send
external toString: t => string = "toString"

@val
external asyncIterator: t = "Symbol.asyncIterator"
@val
external hasInstance: t = "Symbol.hasInstance"
@val external isConcatSpreadable: t = "Symbol.isConcatSpreadable"
@val external iterator: t = "Symbol.iterator"
@val external match: t = "Symbol.match"
@val external matchAll: t = "Symbol.matchAll"
@val external replace: t = "Symbol.replace"
@val external search: t = "Symbol.search"
@val external species: t = "Symbol.species"
@val external split: t = "Symbol.split"
@val external toPrimitive: t = "Symbol.toPrimitive"
@val external toStringTag: t = "Symbol.toStringTag"
@val external unscopables: t = "Symbol.unscopables"
Loading