tryRead

open fun <A> TMVar<A>.tryRead(): A?

Same as TMVar.read except that it returns null if the TMVar is empty and thus never retries.

import arrow.fx.stm.TMVar
import arrow.fx.stm.atomically

suspend fun main() {
//sampleStart
val tmvar = TMVar.empty<Int>()
val result = atomically {
tmvar.tryRead()
}
//sampleEnd
println("Result $result")
}

See also

for a function that retries if the TMVar is empty.

for a function that leaves the TMVar empty after reading.


open fun <A> TQueue<A>.tryRead(): A?

Same as TQueue.read except it returns null 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.tryRead()
}
//sampleEnd
println("Result $result")
println("Items in queue ${atomically { tq.flush() }}")
}

This function never retries.