WeakRef
A weak reference to T: holds its referent without keeping it alive.
get returns the referent, or null once it has been collected or after clear. Check isWeakSupported before leaning on collection for correctness.
Platform behaviour:
JVM, Android, Apple/Native: the platform's own weak reference.
JS: the ES2021
WeakRefglobal. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) thatWeakRefwill not accept, the referent is held strongly instead.
This is the engine's only expect/actual. WeakMap and WeakSet need it and there is no multiplatform weak reference in the standard library (D-52).
A weak reference to T: holds its referent without keeping it alive.
get returns the referent, or null once it has been collected or after clear. Check isWeakSupported before leaning on collection for correctness.
Platform behaviour:
JVM, Android, Apple/Native: the platform's own weak reference.
JS: the ES2021
WeakRefglobal. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) thatWeakRefwill not accept, the referent is held strongly instead.
This is the engine's only expect/actual. WeakMap and WeakSet need it and there is no multiplatform weak reference in the standard library (D-52).
A weak reference to T: holds its referent without keeping it alive.
get returns the referent, or null once it has been collected or after clear. Check isWeakSupported before leaning on collection for correctness.
Platform behaviour:
JVM, Android, Apple/Native: the platform's own weak reference.
JS: the ES2021
WeakRefglobal. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) thatWeakRefwill not accept, the referent is held strongly instead.
This is the engine's only expect/actual. WeakMap and WeakSet need it and there is no multiplatform weak reference in the standard library (D-52).
A weak reference to T: holds its referent without keeping it alive.
get returns the referent, or null once it has been collected or after clear. Check isWeakSupported before leaning on collection for correctness.
Platform behaviour:
JVM, Android, Apple/Native: the platform's own weak reference.
JS: the ES2021
WeakRefglobal. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) thatWeakRefwill not accept, the referent is held strongly instead.
This is the engine's only expect/actual. WeakMap and WeakSet need it and there is no multiplatform weak reference in the standard library (D-52).
A weak reference to T: holds its referent without keeping it alive.
get returns the referent, or null once it has been collected or after clear. Check isWeakSupported before leaning on collection for correctness.
Platform behaviour:
JVM, Android, Apple/Native: the platform's own weak reference.
JS: the ES2021
WeakRefglobal. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) thatWeakRefwill not accept, the referent is held strongly instead.
This is the engine's only expect/actual. WeakMap and WeakSet need it and there is no multiplatform weak reference in the standard library (D-52).
Wasm has no weak reference. A Kotlin object on Wasm lives in the Wasm heap, not the JavaScript one, so the WeakRef global cannot hold it, and the standard library offers nothing else.
The referent is therefore held strongly until clear. Nothing a script can do notices: a WeakMap has no iteration and no size, so whether an entry was collected is unobservable. The cost is memory, not behaviour. isWeakSupported is false here, which is how a host finds out.