ContextualValSuspend

class ContextualValSuspend<T>(val name: String, var default: suspend (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 ContextualVal for a blocking version (not suspend).

Constructors

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

Types

Link copied to clipboard

Properties

Link copied to clipboard
var default: suspend (CoroutineContext) -> T
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
suspend fun get(): T

Gets the value for the current coroutineContext.

suspend fun get(context: CoroutineContext): T

Gets the value for the given context.

suspend 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.