Hi all! For Arrow.Fx does it make sense to have al...
# arrow
t
Hi all! For Arrow.Fx does it make sense to have all impure functions have IO return types (in addition to the
suspend
modifier) or is it best practice to just use IO to run the effects at the boundary of the system and stick to
suspend
to indicate impurity? For example
suspend fun printAString(s: String): Unit
suspend fun printAString(s: String): IO<Unit>
At the moment I am attempting to stick to a ports and adapters architecture with
suspend
for impure functions generally,
IO
return types at the service level, and actually unwrapping/running the IO at the controllers. The data is also the boundary but it didn't make a lot of sense to me to unwrap anything there so I've just been using
suspend
in the repos. As a follow-up, does anyone know if it is possible to custom alias the modifier to something like
impure
to distinguish from coroutine suspend methods? Or am I thinking about that incorrectly? Like
impure fun printAString(s: String): Unit
Is that something we might want to use Arrow.Meta for? Thanks!
m
Should you second example be
fun printAString(s: String): IO<Unit>
? In my view, returning
IO
documents sufficiently that the function has side effects.
t
As these operations occur at the boundary, why wouldn't you resolve the IO inside this function and return an Either/Option rather than IO itself? Unless I'm missing something, the IO has to be resolved at some point and it seem to me at the boundary seems like a good place to do this?
s
you would either use
suspend
OR
IO
, both
suspend
and
IO
doesn't really make sense 🙂
you can also convert from
suspend
to
IO
using
IO.effect
or from
IO
to
suspend
using
.suspended()
t
thanks for the responses! I will talk with my team about what is preferred: IO return types or suspend. @tim sorry I should clarify that they are not occurring at the boundary, just running the effects on the boundary. So like a service method returns side effects so I'm running them at the controller.
t
I definitely don't understand this well enough 🙂
s
in certain cases you have to use
IO
, e.g. switching threads, racing,
parMap
,
parTraverse
etc....