https://kotlinlang.org logo
Title
l

Lukasz Kalnik

08/03/2022, 3:20 PM
What is the advantage of using
either
over
either.eager
for suspending computations? If I use
either.eager
, will it simply block the current thread?
suspend fun doSomethingLong(): Either<Error, Unit>

either.eager {
    doSomethingLong() // does it block the current thread?
}
s

Sam

08/03/2022, 3:20 PM
Does that even compile?
l

Lukasz Kalnik

08/03/2022, 3:20 PM
Yes
public final inline fun <E, A> eager(
    crossinline f: suspend EagerEffectScope<E>.() → A
): Either<E, A>
s

simon.vergauwen

08/03/2022, 3:23 PM
That should not compile
EagerEffectScope
is marked
@RestrictSuspension
l

Lukasz Kalnik

08/03/2022, 3:24 PM
It compiles at least inside of another
suspend
function, maybe that's the catch
s

simon.vergauwen

08/03/2022, 3:26 PM
Ah yes. It’s
inline
so it uses the
suspend
fro the outer scope.
l

Lukasz Kalnik

08/03/2022, 3:26 PM
Ah ok
Thanks!
That explains it.
s

simon.vergauwen

08/03/2022, 3:27 PM
Hmm.. It’s not compiling for me though 🤯
Where is your
suspend
call?
There is no
suspend
call inside
eager
in your screenshot
Only the call to
bind
but that should be coming from
eager
l

Lukasz Kalnik

08/03/2022, 3:28 PM
So the IDE shows
bind()
as the suspend function...
s

simon.vergauwen

08/03/2022, 3:28 PM
Yes, but that is coming from
EagerEffectScope
so it’s allowed
delay(100)
should not be allowed
l

Lukasz Kalnik

08/03/2022, 3:29 PM
Makes sense, I just misinterpreted the "suspend" marker in the gutter
Thanks for the explanation!
Ok, so in this case, is there a difference if I use
either
or
either.eager
? I suppose not. Or is there a reason why I should use
either
, e.g. because my outer function is suspending anyway?
s

simon.vergauwen

08/03/2022, 3:31 PM
In this case there is no difference at all. You’re free to use both.