WeakRef

actual class WeakRef<T : Any>(referred: T)

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 WeakRef global. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) that WeakRef will 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).

expect class WeakRef<T : Any>(referred: T)

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 WeakRef global. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) that WeakRef will 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).

actual class WeakRef<T : Any>(referred: T)

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 WeakRef global. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) that WeakRef will 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).

actual class WeakRef<T : Any>(referred: T)

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 WeakRef global. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) that WeakRef will 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).

actual class WeakRef<T : Any>(referred: T)

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 WeakRef global. On an older runtime, and for referents backed by JS primitives (String, boxed numbers) that WeakRef will 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).

actual class WeakRef<T : Any>(referred: T)

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.

Constructors

Link copied to clipboard
actual constructor(referred: T)
expect constructor(referred: T)
actual constructor(referred: T)
actual constructor(referred: T)
actual constructor(referred: T)
actual constructor(referred: T)

Types

Link copied to clipboard
actual object Companion
expect object Companion
actual object Companion
actual object Companion
actual object Companion
actual object Companion

Functions

Link copied to clipboard
actual fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

expect fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

actual fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

actual fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

actual fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

actual fun clear()

Drops the reference, so get returns null from here on. Calling it twice is fine.

Link copied to clipboard
actual fun get(): T?

The referent, or null once collected or cleared.

expect fun get(): T?

The referent, or null once collected or cleared.

actual fun get(): T?

The referent, or null once collected or cleared.

actual fun get(): T?

The referent, or null once collected or cleared.

actual fun get(): T?

The referent, or null once collected or cleared.

actual fun get(): T?

The referent, or null once collected or cleared.