Can someone explain the differences between `Opti...
# arrow
f
Can someone explain the differences between
Option.else
vs
Option.getOrElse
and
Either.filterOrElse
vs
Either.filterOrOther
i
which version are you running?
f
0.9.0
is that bad 😬
i
no not at all
s
@Fred Friis if you check the signature it should be like.
fun <A> Option<A>.else(f: () -> Option<A>): Option<A>
fun <A> Option<A>.getOrElse(f: () -> A): A
So
getOrElse
works similar to
?:
but instead of going from
A?
->
A
you go from
Option<A>
to
A
.
else
works similarly but it doesn’t unwrap, so the result remains wrapped within
Option
. Similar to
null ?: null
, which is not allowed with
getOrElse
. So while
?:
can deal with more different situations the return type depends on the argument, which is not the case with
else
or
getOrElse
.
Oh you already got an other answer… 😅
f
@simon.vergauwen still much appreciated!
👍 1
m
Either.filterOrElse
and
Either.filterOrOther
are the same under the covers except for one aspect. If the
predicate
is false, both call
default
.
filterOrElse
does not pass anything whereas
filterOrOther
passes the value of the Either. I added this as I was using filtering for error handling, and wanted to include information about the filtered out value in my errors.
filterOrOther
allows it to flow nicely, where
filterOrElse
required having the variable available somewhere in the more general scope.