Either<L, R>
is a representation of one of two possible values L
or R
, but not both at the same time. Either
is a sealed class (similar to Option
) with two subtypes Left<L>
and Right<R>
. Usually Either
is used to represent results that can fail, using the left side to represent the error and the right side to represent a successful result. Because representing operations that can fail is a common scenario, Arrow's Either
is right biased, in other words, unless it is documented otherwise all operations run on the right side.
Let's translate our division example from Option
to Either
:
import arrow.core.Either import arrow.core.Either.Right import arrow.core.Either.Left fun eitherDivide(num: Int, den: Int): Either<String, Int> { val option = optionDivide(num, den) return when (option) { is Some -> Right(option.t) None -> Left("$num isn't divisible by $den") } }
Now instead of returning a None
value, we're returning valuable information...