WeakRef

expect fun <T> WeakRef(value: T): WeakRef<T>(source)

Instantiates a weak reference: a reference to a value that doesn't stop the garbage collector from collecting it.

The value may be accessed with WeakRef.read. However, it could disappear at any time.

When should you use a weak reference?

The runtime is encouraged to clear the value as soon as possible.

This makes implementations ideal for writing mappers from an object to a more expensive one, when we know that we don't need the result of the mapping as soon as the initial object becomes unavailable.

The exact behavior of the returned object is platform-specific.

See also

For implementing caches

actual fun <T> WeakRef(value: T): WeakRef<T>(source)

Implementation of WeakRef backed by a JS WeakRef.

JS doesn't make a difference between weak and soft references.

actual fun <T> WeakRef(value: T): WeakRef<T>(source)

Implementation of WeakRef backed by a JVM WeakReference.

actual fun <T> WeakRef(value: T): WeakRef<T>(source)

Implementation of WeakRef backed by a native WeakReference.

Kotlin Native doesn't make a difference between weak and soft references.

actual fun <T> WeakRef(value: T): WeakRef<T>(source)

Implementation of WeakRef backed by a JS WeakRef.

JS doesn't make a difference between weak and soft references.