write

abstract fun <A> TVar<A>.write(a: A)(source)

Set the value of a TVar.

import arrow.fx.stm.TVar
import arrow.fx.stm.atomically

suspend fun main() {
//sampleStart
val tvar = TVar.new(10)
val result = atomically {
tvar.write(20)
}
//sampleEnd
println(result)
}

Similarly to read this comes with a few guarantees:

  • For multiple writes to the same TVar in a transaction only the last will actually be performed

  • When committing the value inside the TVar, at the time of calling write, has to be the same as the current value otherwise the transaction will retry


open fun <A> TQueue<A>.write(a: A)(source)

Append an element to the TQueue.

import arrow.fx.stm.TQueue
import arrow.fx.stm.atomically

suspend fun main() {
//sampleStart
val tq = TQueue.new<Int>()
atomically {
tq.write(2)
}
//sampleEnd
println("Items in queue ${atomically { tq.flush() }}")
}

This function never retries.