Skip to content

Commit a3bce80

Browse files
WeakMap docstrings
1 parent c3dc0dc commit a3bce80

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

packages/@rescript/runtime/Stdlib_WeakMap.res

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,84 @@
1+
/***
2+
Bindings to JavaScript's `WeakMap`.
3+
4+
Weak maps keep key/value pairs where keys must be objects and the references do not prevent garbage collection.
5+
*/
6+
7+
/** Mutable weak map storing values of type `'v` with object keys `'k`. */
18
@notUndefined
29
type t<'k, 'v>
310

11+
/**
12+
Creates an empty weak map.
13+
14+
See [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) on MDN.
15+
16+
## Examples
17+
18+
```rescript
19+
let cache = Stdlib_WeakMap.make()
20+
Stdlib_WeakMap.get(cache, Stdlib_Object.make()) == None
21+
```
22+
*/
423
@new external make: unit => t<'k, 'v> = "WeakMap"
524

25+
/**
26+
`get(map, key)` returns `Some(value)` when `key` exists, otherwise `None`.
27+
28+
## Examples
29+
30+
```rescript
31+
let cache = Stdlib_WeakMap.make()
32+
let key = Stdlib_Object.make()
33+
Stdlib_WeakMap.get(cache, key) == None
34+
let _ = Stdlib_WeakMap.set(cache, key, "user")
35+
Stdlib_WeakMap.get(cache, key) == Some("user")
36+
```
37+
*/
638
@send external get: (t<'k, 'v>, 'k) => option<'v> = "get"
39+
40+
/**
41+
`has(map, key)` checks whether `key` exists in the weak map.
42+
43+
## Examples
44+
45+
```rescript
46+
let cache = Stdlib_WeakMap.make()
47+
let key = Stdlib_Object.make()
48+
Stdlib_WeakMap.has(cache, key) == false
49+
let _ = Stdlib_WeakMap.set(cache, key, ())
50+
Stdlib_WeakMap.has(cache, key) == true
51+
```
52+
*/
753
@send external has: (t<'k, 'v>, 'k) => bool = "has"
54+
55+
/**
56+
`set(map, key, value)` stores `value` for `key` and returns the map for chaining.
57+
58+
## Examples
59+
60+
```rescript
61+
let cache = Stdlib_WeakMap.make()
62+
let key = Stdlib_Object.make()
63+
let _ = Stdlib_WeakMap.set(cache, key, 42)
64+
Stdlib_WeakMap.get(cache, key) == Some(42)
65+
```
66+
*/
867
@send external set: (t<'k, 'v>, 'k, 'v) => t<'k, 'v> = "set"
68+
69+
/**
70+
`delete(map, key)` removes `key` and returns `true` if an entry existed.
71+
72+
## Examples
73+
74+
```rescript
75+
let cache = Stdlib_WeakMap.make()
76+
let key = Stdlib_Object.make()
77+
Stdlib_WeakMap.delete(cache, key) == false
78+
let _ = Stdlib_WeakMap.set(cache, key, 1)
79+
Stdlib_WeakMap.delete(cache, key) == true
80+
```
81+
*/
982
@send external delete: (t<'k, 'v>, 'k) => bool = "delete"
1083

1184
/**

0 commit comments

Comments
 (0)