WeakRef

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

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected
actual class WeakRef<T : Any>(referred: T)

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected
expect class WeakRef<T : Any>(referred: T)

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected
actual class WeakRef<T : Any>(referred: T)

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected
actual class WeakRef<T : Any>(referred: T)

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected
actual class WeakRef<T : Any>(referred: T)

A weak reference to T. Holds its referent without preventing garbage collection.

get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.

Platform behavior:

  • JVM, Android, Apple/Native: backed by the platform weak reference.

  • Kotlin/JS: backed by the ES2021 WeakRef global. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not valid WeakRef targets, the referent is held strongly.

  • Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is false.

val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected

Constructors

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

Types

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

Functions

Link copied to clipboard
actual fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

actual fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

expect fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

actual fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

actual fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

actual fun clear()

Drops the reference. Subsequent calls to get return null. Idempotent.

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

The referent, or null if it has been collected or cleared.

actual fun get(): T?

The referent, or null if it has been collected or cleared.

expect fun get(): T?

The referent, or null if it has been collected or cleared.

actual fun get(): T?

The referent, or null if it has been collected or cleared.

actual fun get(): T?

The referent, or null if it has been collected or cleared.

actual fun get(): T?

The referent, or null if it has been collected or cleared.