Skip to content

Key[T]

Stores a strand-local value.

Constructor

Key[U]() overload 1 of 2

Creates a strand-local key. An initial value is returned in a strand until that strand sets the key. Without one, the initial value is nil.

let key = strand.Key 0

Parameters

NameTypeDescription
self Key[U | nil] Receiver.

Fields

value @ T [get, set]

Gets or sets the value associated with this key in the current strand.

Returns the initial value when the key has not been set in the current strand.

let key = strand.Key()
assert_eq $key.value nil

key.value = "parent"
assert_eq $key.value "parent"

let results = strand.fork
  do
    assert_eq $key.value "parent"
    key.value = "child"
    key.value

assert_eq $results (Tuple(["child"]))
assert_eq $key.value "parent"
Open in playground

Methods

with[R] value block -> R

Runs block with a temporary value for this key in the calling strand. Restores the previous binding when the block returns or raises. Child strands created inside the block inherit the temporary value.

Parameters

NameTypeDescription
value T Temporary value for this key.
block (() -> R) Block to call with the temporary value.

Example

let depth = strand.Key 0
depth.with 1 do
  echo $depth.value