arrow-fx-coroutines / arrow.fx.coroutines / ConcurrentVar / tryPut

tryPut

abstract suspend fun tryPut(a: A): Boolean

Tries to put A in the ConcurrentVar if it is empty, returns immediately with true if successfully put the value in the ConcurrentVar or false otherwise.

import arrow.fx.coroutines.*

suspend fun main(): Unit {
  //sampleStart
  val mvar = ConcurrentVar.empty<Int>()
  val succeed = mvar.tryPut(5)
  val failed = mvar.tryPut(10)
  val res = mvar.take()
  //sampleEnd
  println("succeed: $succeed, failed: $failed, res: $res")
}

See Also

tryPut

Do you like Arrow?

Arrow Org
<