https://kotlinlang.org logo
#arrow
Title
# arrow
t

Tom Davis

09/19/2023, 1:55 PM
is there a function like
Either<Throwable, T>.getOrThrow(): T
? i've been using
when()
to change behavior on
Left
or
Right
but it's kinda verbose.
p

pakoito

09/19/2023, 2:01 PM
throwing is discouraged, so when/fold is the way to go if you want to do it 😄
t

Tom Davis

09/19/2023, 2:01 PM
gotcha. i don't want to, but that's the controller api 😉
a

Alejandro Serrano.Mena

09/19/2023, 3:01 PM
in those cases you can do
theEitherValue.getOrElse { throw it }
, which I find quite readable and not so verbose
6
c

CLOVIS

09/20/2023, 7:53 AM
@Alejandro Serrano.Mena any preference between
either.getOrElse { throw it }
and
either.recover { throw it }
? To my knowledge, these behave the same?
a

Alejandro Serrano.Mena

09/20/2023, 8:01 AM
I tend to use the first one, mostly out of taste (and it also feels a bit weird to say that "throwing" is recovering from an exception 😅) also lately I'm trying to use
Raise<Error>.f(): A
more often than
Either<Error, A>
, and in that case I just use
fold
to make a final throw
3 Views