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.

delete

RESCRIPT
let delete: (t<'k, 'v>, 'k) => bool

delete(map, key) removes key and returns true if an entry existed.

See WeakMap.prototype.delete on MDN.

Examples

RESCRIPT
let cache = WeakMap.make() let key = Object.make() WeakMap.delete(cache, key) == false let _ = WeakMap.set(cache, key, 1) WeakMap.delete(cache, key) == true

get

RESCRIPT
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

RESCRIPT
let 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

RESCRIPT
let has: (t<'k, 'v>, 'k) => bool

has(map, key) checks whether key exists in the weak map.

See WeakMap.prototype.has on MDN.

Examples

RESCRIPT
let cache = WeakMap.make() let key = Object.make() WeakMap.has(cache, key) == false let _ = WeakMap.set(cache, key, ()) WeakMap.has(cache, key) == true

ignore

RESCRIPT
let ignore: t<'k, 'v> => unit

ignore(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.

make

RESCRIPT
let make: unit => t<'k, 'v>

Creates an empty weak map.

See WeakMap on MDN.

Examples

RESCRIPT
let cache = WeakMap.make() WeakMap.get(cache, Object.make()) == None

set

RESCRIPT
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

RESCRIPT
let cache = WeakMap.make() let key = Object.make() let _ = WeakMap.set(cache, key, 42) WeakMap.get(cache, key) == Some(42)

t

RESCRIPT
type t<'k, 'v>

Mutable weak map storing values of type 'v with object keys 'k.