Hi
iam trying to switch to Arrow exception handing within my current android application
it appears to working ok up to now
i would appreciate some advice on the following code
is this a valid use of "`either {}`" with a single "`bind()`" containded within it?
is there any "better" alternative?
Copy code
val work: Either<DatasourceException, *> = either { persistOutcome().bind() }
r
raulraja
03/01/2022, 1:51 PM
hi @Tower Guidev2, that would be the same as just:
Copy code
val work = persistOutcome()
s
simon.vergauwen
03/01/2022, 1:51 PM
Hey @Tower Guidev2,
Having
either { }
with a single
bind()
is redundant and equivalent to
persistentOutcome()
.
If you're trying to capture any unexpected exceptions you should use
Either.catch
and
mapLeft
to map
Throwable
to your error domain.
➕ 2
👍 1
t
Tower Guidev2
03/01/2022, 1:52 PM
thanks for confirming i was "correct" to feel uneasy about this 😄
y
Youssef Shoaib [MOD]
03/01/2022, 4:09 PM
remember 2 things:
1. Every call to bind inside an effect block (e.g. an either block) corresponds to a flatMap call
2. The return value of an effect block corresponds to the "preferred" or Right side of the effect type (e.g. Either.right)
and so, the code examples is practically equivalent to this:
Copy code
persistentOutcome().flatMap { Either.Right(it) }
and so you're turning a Right result into a Right result of the same exact value and a Left result into itself, and so in both cases it's just a more-convoluted