RefUnion
Ref
Ref<'T> holds a mutable value that concurrent workflows update atomically. Create one with
make, read it with get, and write it with
set or the atomic update family. Read the Ref guide for
the atomicity and get/set pitfalls.
Signature reference
| Function | Signature | Description |
|---|---|---|
make |
'T -> Flow<'env, 'none, Ref<'T>> |
Creates a new reference with an initial value. |
get |
Ref<'T> -> Flow<'env, 'none, 'T> |
Reads the current value of the reference. |
set |
'T -> Ref<'T> -> Flow<'env, 'none, unit> |
Sets the value of the reference to a new value. |
update |
('T -> 'T) -> Ref<'T> -> Flow<'env, 'none, unit> |
Atomically updates the value using the supplied function. |
modify |
('T -> 'v * 'T) -> Ref<'T> -> Flow<'env, 'none, 'v> |
Atomically updates the value and returns a derived result. |
getAndSet |
'T -> Ref<'T> -> Flow<'env, 'none, 'T> |
Sets the value and returns the value it held before the update. |
getAndUpdate |
('T -> 'T) -> Ref<'T> -> Flow<'env, 'none, 'T> |
Updates the value and returns the value it held before the update. |
updateAndGet |
('T -> 'T) -> Ref<'T> -> Flow<'env, 'none, 'T> |
Updates the value and returns the value after the update. |
The refs torture test runs every update operation from eight fibers at once and checks that no update is lost and no two see the same value.
Specification
Kind
Union
Members
8
Examples
0
makeSignature
Ref.make value
getSignature
Ref.get reference
setSignature
Ref.set value reference
updateSignature
Ref.update f reference
modifySignature
Ref.modify f reference
Summary
| Name | Signature | Synopsis |
|---|---|---|
| Operations | ||
| make | Ref.make value | Creates a new Ref with the initial value. |
| get | Ref.get reference | Reads the current value of the reference. |
| set | Ref.set value reference | Sets the value of the reference to the specified value. |
| update | Ref.update f reference | Updates the value of the reference using the supplied function. |
| modify | Ref.modify f reference | Updates the value of the reference using the supplied function and returns a derived value. |
| getAndSet | Ref.getAndSet value reference | Sets the value of the reference and returns the value it held before the update. |
| getAndUpdate | Ref.getAndUpdate f reference | Updates the value using the supplied function and returns the value it held before the update. |
| updateAndGet | Ref.updateAndGet f reference | Updates the value using the supplied function and returns the value after the update. |
Operations
kind:member
Creates a new Ref`1 with the initial value.
Parameters
| Name | Type | Description |
|---|---|---|
| value | 'T | The initial value of the reference. |
Returns
Flow<'env, 'none, Ref<'T>>
Verification Examples
(Ref.make 10).RunSynchronously(())kind:member
Reads the current value of the reference.
Parameters
| Name | Type | Description |
|---|---|---|
| reference | Ref<'T> | The Ref`1 to read from. |
Returns
Flow<'env, 'none, 'T>
Verification Examples
let readInitialValue () =
flow {
let! reference = Ref.make 10
return! Ref.get reference
}kind:member
Sets the value of the reference to the specified value.
Parameters
| Name | Type | Description |
|---|---|---|
| value | 'T | The new value to set. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, unit>
Verification Examples
let replaceValue () =
flow {
let! reference = Ref.make 10
do! Ref.set 20 reference
}kind:member
Updates the value of the reference using the supplied function.
Parameters
| Name | Type | Description |
|---|---|---|
| f | 'T -> 'T | The update function of type 'T -> 'T. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, unit>
Verification Examples
Ref.update (fun x -> x + 1) myRefkind:member
Updates the value of the reference using the supplied function and returns a derived value.
Parameters
| Name | Type | Description |
|---|---|---|
| f | 'T -> 'v * 'T | The update function of type 'T -> 'v * 'T, returning the result before the next state. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, 'v>
Verification Examples
Ref.modify (fun x -> "increased", x + 1) myRefkind:member
Sets the value of the reference and returns the value it held before the update.
Parameters
| Name | Type | Description |
|---|---|---|
| value | 'T | The new value to set. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, 'T>
Verification Examples
let replaceAndReturnPrevious () =
flow {
let! reference = Ref.make 10
return! Ref.getAndSet 20 reference
}kind:member
Updates the value using the supplied function and returns the value it held before the update.
Parameters
| Name | Type | Description |
|---|---|---|
| f | 'T -> 'T | The update function of type 'T -> 'T. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, 'T>
Verification Examples
Ref.getAndUpdate (fun x -> x + 1) myRefkind:member
Updates the value using the supplied function and returns the value after the update.
Parameters
| Name | Type | Description |
|---|---|---|
| f | 'T -> 'T | The update function of type 'T -> 'T. |
| reference | Ref<'T> | The Ref`1 to update. |
Returns
Flow<'env, 'none, 'T>
Verification Examples
Ref.updateAndGet (fun x -> x + 1) myRef
