There's something I don't quiet understand, corout...
# arrow
b
There's something I don't quiet understand, coroutines are an implementation of the Continuation monad, right? Then why is it recommended to use them instead of the IO monad?
r
The continuation monad can model all other monads including IO. Coroutines is the runtime of how continuations are implemented in Kotlin. Consider:
Copy code
typealias IO<A> = suspend () -> A
Everything you can do with IO you can do with suspend and with better syntax and more performant since it removes the need for map and flatMap yet still lets you treat programs as values since they are still just functions. Also enforces that errors can’t be raised uncontrolled since the continuation forces you to deal with the potential throwable (same as IO)
👍 2
☝️ 1
IO as a data type is IMO unergonomic but also inefficient compared to suspend functions and the only reason why you find in places like Scala is because the lang does not explicitly provide a way to track effects without wrapped types. Even scala/async took the approach to rely on Future to provide direct syntax, suspend in contrast is based on compiler CPS transformations, that is what IO would look like in a lang with continuations.
The IO monad as a data type in the JVM it incurs in additional allocations because it represents actions as values of a sealed hierarchy which capture the user intention in lambdas and later are interpreted and unfolded. That compared to continuations in Kotlin and CPS compiler desugaring where is mostly stack based until you go async is a huge win in performance and easier style to write for users.
b
The continuation monad can model all other monads including IO. What do you mean by this?
that shows any monafic effect can be built with continuations
b
I will give this a read. Thanks a lot Raul! Very helpful as always
r
arrow-continuations includes reset/shift which abstract away this abilty and Arrow uses it to build things like
either
comprehensions and others
no problem!
k
Hi, concerning this Continuation over IO monad, I'm currently trying to "switch" my `IO`s to `supend`s, but I'm confused as to what steps I should take. Is this explained someplace? Should I use Arrow-fx Coroutines? What are the changes at the program edges?…
r
hi @kluck https://arrow-kt.io/docs/next/effects/io/ and https://arrow-kt.io/docs/next/fx/async/ cover many of the operators you find in both cases. For the most part you get rid of flatMap, map and others in favor function application and most apps choose to model their functions as
suspend fun someBizService(): Either<YourErrorType, YourResultType>
in that same menu for the next iteration you can also find Schedule and other data types that are now with suspend. You only need arrow-fx-coroutines for most cases.
if a particular use case gives you trouble we can look at it together here and advice how to best port it to fx coroutines.
☝️ 1
k
Thanks for the guidelines, I haven't actually started yet, but I'll keep you posted when I run into an issue ;)