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

NameSignatureSynopsis
Operations
makeRef.make valueCreates a new Ref with the initial value.
getRef.get referenceReads the current value of the reference.
setRef.set value referenceSets the value of the reference to the specified value.
updateRef.update f referenceUpdates the value of the reference using the supplied function.
modifyRef.modify f referenceUpdates the value of the reference using the supplied function and returns a derived value.
getAndSetRef.getAndSet value referenceSets the value of the reference and returns the value it held before the update.
getAndUpdateRef.getAndUpdate f referenceUpdates the value using the supplied function and returns the value it held before the update.
updateAndGetRef.updateAndGet f referenceUpdates the value using the supplied function and returns the value after the update.

Operations

kind:member

make

Ref.make value
Member
Creates a new Ref`1 with the initial value.

Parameters

NameTypeDescription
value'TThe initial value of the reference.

Returns

Flow<'env, 'none, Ref<'T>>

Verification Examples

(Ref.make 10).RunSynchronously(())
kind:member

get

Ref.get reference
Member
Reads the current value of the reference.

Parameters

NameTypeDescription
referenceRef<'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

set

Ref.set value reference
Member
Sets the value of the reference to the specified value.

Parameters

NameTypeDescription
value'TThe new value to set.
referenceRef<'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

update

Ref.update f reference
Member
Updates the value of the reference using the supplied function.

Parameters

NameTypeDescription
f'T -> 'TThe update function of type 'T -> 'T.
referenceRef<'T>The Ref`1 to update.

Returns

Flow<'env, 'none, unit>

Verification Examples

Ref.update (fun x -> x + 1) myRef
kind:member

modify

Ref.modify f reference
Member
Updates the value of the reference using the supplied function and returns a derived value.

Parameters

NameTypeDescription
f'T -> 'v * 'TThe update function of type 'T -> 'v * 'T, returning the result before the next state.
referenceRef<'T>The Ref`1 to update.

Returns

Flow<'env, 'none, 'v>

Verification Examples

Ref.modify (fun x -> "increased", x + 1) myRef
kind:member

getAndSet

Ref.getAndSet value reference
Member
Sets the value of the reference and returns the value it held before the update.

Parameters

NameTypeDescription
value'TThe new value to set.
referenceRef<'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

getAndUpdate

Ref.getAndUpdate f reference
Member
Updates the value using the supplied function and returns the value it held before the update.

Parameters

NameTypeDescription
f'T -> 'TThe update function of type 'T -> 'T.
referenceRef<'T>The Ref`1 to update.

Returns

Flow<'env, 'none, 'T>

Verification Examples

Ref.getAndUpdate (fun x -> x + 1) myRef
kind:member

updateAndGet

Ref.updateAndGet f reference
Member
Updates the value using the supplied function and returns the value after the update.

Parameters

NameTypeDescription
f'T -> 'TThe update function of type 'T -> 'T.
referenceRef<'T>The Ref`1 to update.

Returns

Flow<'env, 'none, 'T>

Verification Examples

Ref.updateAndGet (fun x -> x + 1) myRef