you can call `map` and instead of returning a valu...
# arrow
p
you can call
map
and instead of returning a value, do a side-effect that returns `Unit`inside. It’s not a good pattern but that fits every use case for
ifRight
d
Yes, I figured that out, however it's not that explicit. Also if the last line in the lambda is a statement and not an expression, then I have to explicitly use Unit
Also - please use threads
p
I thought statements returned Unit in Kotlin, I may be wrong
but yes, if you’re doing a side-effect then the type you’re using is
Unit
if you want to be explicit you can always alias
inline fun <E,A> Either<E, A>.sideEffectIfRight(f: (A) -> Unit): Either<E, Unit> = map(f)
s
Shouldn’t flatmap be used if there is side effect to be done on the right hand side ?
p
at that point I’d just make the whole snippet suspended, yes
the new APIs for fx are made for this
Copy code
either {
  someEither.bind()
  suspendSideEffect()
}
I believe that works on 0.11
I don’t know whether it’s there now or will be for the 1.0 rewrite
@simon.vergauwen or @aballano may know better
either way, if you’re in suspend
Either.catch
can help
s
Yes. But think with suspendSideEffect() we would need to use .bind() as well right ? Else it wouldn’t execute ? Or basically coroutines result will not be returned, and we wouldn’t know if this instruction is executed or not.
I always wrap apis that can throw in Either.catch
p
in the new
either
blocks you can call suspend functions and they’re caught IIRC
but yes, in any case Either.catch + bind would work
s
Cool. Thanks.
d
Okay, my bad. Statements are evaluated to Unit
I guess I confused it with a previous version
m
Why not add something like .foreach() that is used in Scala for blocking side effect without return value.
p
Because we don’t want to encourage blocking side-effects. You can always do them in any other block like map, traverse or effectMap even.
the advice is to wrap them on pure APIs like suspend functions
m
But sometimes, it is the only thing available, e.g. with GCP client.
p
put them into an IO then
at least it captures exceptions
m
It is possible to wrap in suspend function yes. Isn't IO deprecated?
p
myList.traverse { IO { client.call(it) } }
👍 1
IO
, or
suspend
depends on what you need