When writing a function whose sole purpose is to c...
# arrow
c
When writing a function whose sole purpose is to check some invariant, is it best practice to declare it as:
EffectScope<Failure>.() -> Unit
or:
() -> Either<Failure, Unit>
I think the first one is clearer that "it's just validation code", what do you think? Is there any official recommendation? The first one also doesn't need the user to call
bind
, which they may forget since there is no proper result.
s
Hey @CLOVIS, There is not official recommendation at this time, you can find a more lengthy answer here. https://stackoverflow.com/questions/72277426/which-should-we-choose-between-effect-and-either-as-a-return-type-of-our-busines/72277572#72277572 The recommended way going forward will probably become context receivers, as you mention this won’t suffer from the bind issue and it’ll also be the most generic version. With Arrow 1.x.x:
Copy code
context(EffectScope<Failure>)
suspend fun validate(): Unit = TODO()
With Arrow 2.x.x
suspend
will become optional, depending on whether you use suspend inside or not.
Copy code
context(Raise<Failure>)
(suspend) fun validate(): Unit = TODO()
In Arrow 2.x.x you’ll be able to do
either { validate() }
to turn it into
() -> Either<Failure, Unit>
. Where in Arrow 1.x.x you still need to be explicit about
either.eager
and
EagerEffectScope<Failure>
. I expect Arrow 2.x.x to become stable sooner than context receivers since they’re still JVM experimental atm. You can now use extension functions instead of context receivers