arrow-fx / arrow.fx / MVar / tryPut

tryPut

abstract fun tryPut(a: A): Kind<F, Boolean>

Fill the MVar if we can do it without blocking. Returns true if successfully put the value or false otherwise.

import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap

fun main(args: Array<String>) {
//sampleStart
val mvar = MVar.factoryUncancellable(IO.async())

mvar.empty<Int>().flatMap { v ->
 v.tryPut(5)
}.unsafeRunSync() == true

mvar.just(5).flatMap { v ->
  v.tryPut(10)
}.unsafeRunSync() == false
//sampleEnd
}

Do you like Arrow?

Arrow Org
<