ContextualVal

class ContextualVal<T>(val name: String, var default: (CoroutineContext) -> T)

A val for which the value can be set via the coroutineContext.

This is similar to a thread-local or the "dynamic scope" concept.

The default value is created lazily on demand per CoroutineScope (not globally!). For this to work you have to inject ContextualValRoot into each CoroutineScope that wants to support this class.

The name is only used to help with debugging.

Also see ContextualValSuspend for a suspend based version.

Constructors

Link copied to clipboard
constructor(name: String, default: (CoroutineContext) -> T)

Types

Link copied to clipboard
Link copied to clipboard

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
suspend fun get(): T

Gets the value for the current coroutineContext.

fun get(context: CoroutineContext): T

Gets the value for the given context.

fun get(scope: CoroutineScope): T

Gets the value for the given scope.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard

The returned CoroutineContext.Element can used to set a value via CoroutineScope.plus.

Link copied to clipboard
suspend fun <R> with(value: T, block: suspend () -> R): R

Sets a new value that only exists within block.