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.
Parameters
| Name | Type | Description |
|---|---|---|
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.
Open in playgroundlet 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"
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
| Name | Type | Description |
|---|---|---|
value |
T |
Temporary value for this key. |
block |
(() -> R) |
Block to call with the temporary value. |