Hi folks, sorry to ask what I’m sure is a fairly s...
# arrow
i
Hi folks, sorry to ask what I’m sure is a fairly simple question, but I feel like I’m missing something fundamental here. I’m trying to implement a simple client that uses OAuth, and that will handle expected “failure” cases like, for example, refreshing expired access tokens. To cut to the point, how can the handling of the right vs Left cases in this code be streamlined further? Surely checking isRight is an anti-pattern? 🙂
m
you can use
flatMap {}
,
map {}
and
fold {}
,
fold {}
not is this particular example but you have that for checking
👍 1
i
Yeah, I’m using that in other places, was having trouble getting the return types to match up. After fiddling a bit I’ve got things to here, which seems a lot cleaner:
j
This may be a good use case for something like
inline fun <C> Either<A, B>.flatMapLeft(f: (A) -> Either<C, B>): Either<C, B>
if it does not already exist. I think this was previously done using
handleErrorWith
from MonadError but since that cannot be inline we may need a new function here. The implementation is basically just
swap().flatMap { f(it).swap() }.swap()
👍 2
s
handleErrorWith
should exist for
Either
and it should be
inline
as well.
👍 1
i
Nice!
👍 3