https://kotlinlang.org logo
#arrow
Title
# arrow
r

Riccardo Cardin

11/04/2023, 7:59 PM
Hey, arrow and functional guys, just a question. Following this article, in Kotlin, we can compare the features of the
IO[A]
monad defined in Scala with a
suspend () -> A
function. This is true because Kotlin translates the previous
suspend
function into code using continuations, and it’s guaranteed to return a
Result[A]
. Let’s introduce the
Either[E, A]
into the equation. We can have a
suspend () -> Either[E, A]
(or a
suspend Raise<E>.() -> A
as well). The Kotlin compiler will translate such a function into a function using continuations, ending with the type
Result[Either[E, A]]
. So, a
suspend () -> Either[E, A]
is equivalent to the blueprint of a function that can make side effects (the
suspend
part) and ends with a
Failure(Throwable)
in case of unexpected problems/bugs, with a
Left[E]
in case of handled errors, with a
Right[A]
in case of the happy path occurs. In some way, it’s very similar to the
IO[E, A]
defined in the ZIO library. Is it correct? Thanks a lot gratitude thank you
r

raulraja

11/04/2023, 8:15 PM
I think all your assumptions are correct. Some of the differences between suspend and IO are a bit more nuance at the implementation level. For example stack safety, implicit safe resource handling, scheduler fairness, etc. suspend and IO follow different strategies and decissions on those. Having said that, most of those can be considered implementation details but at the end
suspend () -> A
is the kotlin equivalent to
IO[A]
in other langs and libraries and the main reason why we got rid of
IO<A>
in Arrow in favor of
suspend
.
❤️ 2
🔝 2
3 Views