WeakMap
Bindings to JavaScript's WeakMap.
Weak maps keep key/value pairs where keys must be objects and the references do not prevent garbage collection.
t
type t<'k, 'v>Mutable weak map storing values of type 'v with object keys 'k.
make
let make: unit => t<'k, 'v>Creates an empty weak map.
See WeakMap on MDN.
Examples
RESCRIPTlet cache = WeakMap.make()
WeakMap.get(cache, Object.make()) == None
get
let get: (t<'k, 'v>, 'k) => option<'v>get(map, key) returns Some(value) when key exists, otherwise None.
See WeakMap.prototype.get on MDN.
Examples
RESCRIPTlet cache = WeakMap.make()
let key = Object.make()
WeakMap.get(cache, key) == None
let _ = WeakMap.set(cache, key, "user")
WeakMap.get(cache, key) == Some("user")
has
let has: (t<'k, 'v>, 'k) => boolhas(map, key) checks whether key exists in the weak map.
See WeakMap.prototype.has on MDN.
Examples
RESCRIPTlet cache = WeakMap.make()
let key = Object.make()
WeakMap.has(cache, key) == false
let _ = WeakMap.set(cache, key, ())
WeakMap.has(cache, key) == true
set
let set: (t<'k, 'v>, 'k, 'v) => t<'k, 'v>set(map, key, value) stores value for key and returns the map for chaining.
See WeakMap.prototype.set on MDN.
Examples
RESCRIPTlet cache = WeakMap.make()
let key = Object.make()
let _ = WeakMap.set(cache, key, 42)
WeakMap.get(cache, key) == Some(42)
delete
let delete: (t<'k, 'v>, 'k) => booldelete(map, key) removes key and returns true if an entry existed.
See WeakMap.prototype.delete on MDN.
Examples
RESCRIPTlet cache = WeakMap.make()
let key = Object.make()
WeakMap.delete(cache, key) == false
let _ = WeakMap.set(cache, key, 1)
WeakMap.delete(cache, key) == true
ignore
let ignore: t<'k, 'v> => unitignore(weakMap) ignores the provided weakMap and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.