Hi, I wanted to get some feedback on the approach ...
# arrow
s
Hi, I wanted to get some feedback on the approach for getting an output of a function that returns
IO<Either<InvalidCommand, Command)>>
as an input to a function that takes
Command
as a parameter and returns
IO
Copy code
return EitherT.monad<ForIO, DomainError>(IO.monad()).fx.monad {
      val (command) = EitherT(commandFrom(commandType, commandString))
      val (eventRecord) = EitherT(commandHandler.handle(command).map { it.right() })
      eventRecord
    }.value().fix()
      .map { result -> result.fold({ Response.Error(it.toString()) }, { Response.Success(it) }) }
      .unsafeRunSync()
Does this seem like the right approach?
s
With the new Arrow Fx Coroutines library you can mix
suspend
and
Either
and achieve the same result. Arrow Fx Coroutines exposes the same API as
IO
over
suspend
, and we’re refactoring
Arrow-Core
to be
inline
and expose a
suspend
fx block over data types.
Copy code
Either.fx {
  val (command) = commandFrom(commandType, commandString)
  val (eventRecord) = commandHandler.handle(command).map { it.right() }
}.fold({ Response.Error(it.toString()) }, { Response.Success(it) })
But if you’re not using the
IO
api like here, so you’re not using any of the Arrow Fx typeclasses such as
Concurrent
or
Async
there is no need to use
IO
on top of
Either
. Hard to see from the snippet.
It’s available on the
0.11.0-SNAPSHOT
for Arrow Core & Arrow Fx.
There currently is no alternative to
unsafeRunSync
tho
s
So when you say new Arrow Fx library, you mean the 0.11.0-SNAPSHOT version? We run this code with
unsafeRunSync
in the REST controller endpoint. You say what you recommended above won’t work with
unsafeRunSync
even if bump the version?
s
It’s a new library in the Arrow Fx ecosystem, and will form the core of going forward of more idiomatic FP in Kotlin. There’s a long intro in the PR, and it’s available as a SNAPSHOT atm. It’ll become stable over the summer as we build some other libraries on top 🙂 We still need some sort of
Enviroment
type to expose
unsafeRunSync
RIf your If your REST controller has suport for
suspend
, then you can just call
suspend
function from there. Otherwise if you’re interested in trying the library out you could use a temporary reantrant lock to write
unsafeRunSync
for
suspend
. Or use
startCoroutine
from the standard library, `startCoroutineCoroutine`from the Arrow Fx Coroutines library. Both take a
Continuation
callback with a
CoroutineContext
. The library exposes similar dispatchers as
Arrow Fx IO
such as
ComputationPool
,
IOPool
etc https://github.com/arrow-kt/arrow-fx/pull/169 https://github.com/arrow-kt/arrow-fx/blob/master/arrow-fx-coroutines/src/main/kotlin/arrow/fx/coroutines/Platform.kt#L128 I just noticed the docs aren’t up yet. Even though they’re written. I’ll fix that tomorrow 🙂
I hope this helps.
s
Thank you sir. This was very helpful 🙏
Awesome work by the way.