https://kotlinlang.org logo
#arrow
Title
# arrow
a

asdf asdf

03/19/2023, 7:10 PM
Is there any reason that arrows
either
builder has to be called from a suspending context? It seems to work fine with a non-suspending implementation like this:
Copy code
fun main() {
    val res = nonSuspendingEither {
        if (Random.nextBoolean()) shift(AuthResult.Success(534))
        else AuthResult.Failure("Invalid")
    }
    println(res)
}

@RestrictsSuspension
interface RestrictedEffectScope<E> : EffectScope<E>

fun <E, A> nonSuspendingEither(closure: suspend RestrictedEffectScope<E>.() -> A): Either<E, A> {
    var result: Either<E, A>? = null
    val scope = object : RestrictedEffectScope<E> {
        override suspend fun <B> shift(r: E): B {
            result = Either.Left(r)
            return suspendCoroutine { /*no-op*/ }
        }
    }
    closure.startCoroutine(scope, Continuation(EmptyCoroutineContext) {
        result = Either.Right(it.getOrThrow())
    })
    return result!!
}
s

simon.vergauwen

03/19/2023, 7:35 PM
Hey @asdf asdf, This is an implementation detail in 1.1.x, and you can use
either.eager
to achieve the same as you do here with
@RestrictSuspension
. In fact the implementation is 99% the same. However in 1.2.x we've simplified this, and
either
is simply
inline fun
there. You can use it in
1.1.6-alpha.59
, it's source compatible with 1.1.x given some extra imports 😉
a

asdf asdf

03/19/2023, 7:58 PM
Neat, thanks!
3 Views