what's the usage of ```either { ... } ?``` I can't...
# arrow
i
what's the usage of
Copy code
either { ... } ?
I can't find documents about it, related to
Copy code
Either.fx<A,B> { ... } ?
s
Hey @Ifvwm, What do you mean usage? It works the same as
Either.fx
but it no longer uses the
Monad
interface underneath and works much more efficiently. The
either { }
version also allows for suspension to go through, which wasn't the case with
fx
. If you want a DSL that returns immediately instead of doing
suspend
there is also
either.eager { }
.
i
@simon.vergauwen I don't know it's a DSL, I found this article https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core.computations/either/index.html#either but it doesn't say much more about it, I hope there's a document to tell how to use it and when it's better than Either.fx
s
Yes, the documentation is still a bit lacking in those places 😞 Here is a little bit more info: https://arrow-kt.io/docs/effects/io/#suspend----eithere-a It's 100% better to use than
Either.fx
, and you can use it exactly the same as
Either.fx
. Actually,
either { }
is more powerful than
Either.fx
and can do more.
i
@simon.vergauwen I saw that
Copy code
IO.fx { 
 continueOn(main)
 effect { updateUI }.bind()
 continueOn(backgroud)
 effect { doIO }.bind()
}
if with
Copy code
suspend () -> Either<A,B>
how to express that inside
Copy code
either { ... }?
continueOn is available inside either ?
s
continueOn
is not available anymore in favor of
withContext
from KotlinX.
Copy code
either {
  withContext(main) { updateUI.bind() }
  withContext(background) { doIO.bind() }
}
Assuming that
updateUI
and
doIO
return
Either
and/or are
suspend
i
@simon.vergauwen IO has
Copy code
unsafeRunAsync
KotlinX has
Copy code
CoroutineScope.async(<http://Dispatchers.IO|Dispatchers.IO>) {...}
for async IO, how to do that inside
Copy code
either {...}?
s
You can safely mix-and-match these patterns now.
Copy code
val res: Either<Stirng, Int> =
  either<String, Int> {
     val task: Deferred<Either<String, Int>> = scope.async(<http://Dispatchers.IO|Dispatchers.IO>) { delay(1000); 1.right() }

     ...

     task.await().bind()
  }
i
@simon.vergauwen so KotlinX's
Copy code
runBlocking { ... }
is also availabel inside either { ... } just like scope.async?
s
Yes