asdf asdf
03/19/2023, 7:10 PMeither
builder has to be called from a suspending context? It seems to work fine with a non-suspending implementation like this:
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!!
}
simon.vergauwen
03/19/2023, 7:35 PMeither.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 😉asdf asdf
03/19/2023, 7:58 PM