Can anyone explain me difference between direct in...
# arrow
g
Can anyone explain me difference between direct invocation of suspend function and wrapping it into
effect()
inside ConcurrentSyntax?
k
my understanding is that wrapping normal suspend functions into
effect
removes the eager execution and will be triggered only at the end rendering the suspend function invocation lazy. I believe that is the reason why
Arrow-fx
is created to allow lazy Coroutines evaluations compared to
KotlinX-Coroutines
library
s
effect
is a
@RestrictSuspension
DSL, which means that all foreign effects beside
F
need to explicitly wrapped in
effect
. If we wouldn't do it, then we cannot guarantee that
suspend
functions are safely interleaved into `F`'s execution like @kioba mentioned 🙂
The DSL basically prevents doing the following, whilst still allowing foreign
suspend
effects.
Copy code
val x: IO<Unit> = io.flatMap {
  foreignSuspendFunction()
  IO { otherSupendFunction() }
}
In the above pseudo code (which could be the result without
@RestrictSuspension
or
effect
), you'd see that
foreignSuspendFunction()
gets executed whilst building the
IO
value instead of when running the
IO
value.
However, we felt that this was not very Kotlin idiomatic which was the biggest drive to implement
IO
as a
suspend
library, which took quite a while to crack the correct encoding which offer the same guarantees as
IO
.