TRefType

TRef

TRef<'T> is a transactional reference: a variable read and written inside an STM transaction. Read the STM guide for retry and orElse behavior.

Signature reference

Function Signature Description
make 'T -> STM<TRef<'T>> Creates a new transactional reference.
get TRef<'T> -> STM<'T> Reads the value of a TRef within a transaction.
set 'T -> TRef<'T> -> STM<unit> Sets the value of a TRef within a transaction.
update ('T -> 'T) -> TRef<'T> -> STM<unit> Atomically updates a TRef within a transaction.

Specification

Kind
Type
Members
8
Examples
0
makeSignature
TRef.make value
getSignature
TRef.get tref
setSignature
TRef.set value tref
updateSignature
TRef.update f tref
modifySignature
TRef.modify f tref

Summary

NameSignatureSynopsis
Operations
makeTRef.make valueCreates a new TRef with the initial value within an STM transaction.
getTRef.get trefReads the current value of the transactional reference within a transaction.
setTRef.set value trefSets the value of the transactional reference within a transaction.
updateTRef.update f trefUpdates the value of the transactional reference within a transaction using the supplied function.
modifyTRef.modify f trefAtomically applies f, writes the new state, and returns the value it produced.
getAndSetTRef.getAndSet value trefSets the value and returns the previous value.
getAndUpdateTRef.getAndUpdate f trefUpdates the value and returns the previous value.
updateAndGetTRef.updateAndGet f trefUpdates the value and returns the new value.

Operations

kind:member

make

TRef.make value
Member
Creates a new TRef`1 with the initial value within an STM transaction.

Parameters

NameTypeDescription
value'TThe initial value for the transactional reference.

Returns

STM<TRef<'T>>

Verification Examples

let tx = stm {
    let! counter = TRef.make 0
    return counter
}
kind:member

get

TRef.get tref
Member
Reads the current value of the transactional reference within a transaction.

Parameters

NameTypeDescription
trefTRef<'T>The transactional reference to read.

Returns

STM<'T>

Verification Examples

let tx (counter: TRef<int>) = stm {
    let! value = TRef.get counter
    return value
}
kind:member

set

TRef.set value tref
Member
Sets the value of the transactional reference within a transaction.

Parameters

NameTypeDescription
value'TThe new value to store in the reference.
trefTRef<'T>The transactional reference to update.

Returns

STM<unit>

Verification Examples

let tx (counter: TRef<int>) = stm {
    do! TRef.set 10 counter
}
kind:member

update

TRef.update f tref
Member
Updates the value of the transactional reference within a transaction using the supplied function.

Parameters

NameTypeDescription
f'T -> 'TThe function to apply to the current value to produce the new value.
trefTRef<'T>The transactional reference to update.

Returns

STM<unit>

Verification Examples

let tx (counter: TRef<int>) = stm {
    do! TRef.update (fun n -> n + 1) counter
}
kind:member

modify

TRef.modify f tref
Member
Atomically applies f, writes the new state, and returns the value it produced.

Parameters

NameTypeDescription
f'T -> 'v * 'T
trefTRef<'T>

Returns

STM<'v>
kind:member

getAndSet

TRef.getAndSet value tref
Member
Sets the value and returns the previous value.

Parameters

NameTypeDescription
value'T
trefTRef<'T>

Returns

STM<'T>
kind:member

getAndUpdate

TRef.getAndUpdate f tref
Member
Updates the value and returns the previous value.

Parameters

NameTypeDescription
f'T -> 'T
trefTRef<'T>

Returns

STM<'T>
kind:member

updateAndGet

TRef.updateAndGet f tref
Member
Updates the value and returns the new value.

Parameters

NameTypeDescription
f'T -> 'T
trefTRef<'T>

Returns

STM<'T>