arrow-fx-stm / arrow.fx.stm / STM / read
abstract fun <A>
TVar
<A>.read(): A
Read the value from 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.read()
}
//sampleEnd
println(result)
}
This comes with a few guarantees:
open fun <A>
TMVar
<A>.read(): A
Read a value from a TMVar without removing it.
import arrow.fx.stm.TMVar
import arrow.fx.stm.atomically
suspend fun main() {
//sampleStart
val tmvar = TMVar.new(30)
val result = atomically {
tmvar.read()
}
//sampleEnd
println("Result $result")
println("New value ${atomically { tmvar.tryTake() } }")
}
This retries if the TMVar is empty but does not take the value out if it succeeds.
See Also
open fun <A>
TQueue
<A>.read(): A
Remove the front element from the TQueue or retry if the TQueue is empty.
import arrow.fx.stm.TQueue
import arrow.fx.stm.atomically
suspend fun main() {
//sampleStart
val tq = TQueue.new<Int>()
val result = atomically {
tq.write(2)
tq.read()
}
//sampleEnd
println("Result $result")
println("Items in queue ${atomically { tq.flush() }}")
}
See Also
Do you like Arrow?
✖