I see many users, but I don’t understand the benef...
# android
l
I see many users, but I don’t understand the benefits of Either. What is the difference between Either and Pair in kotlin? https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core/-either/.
d
Probably more suited for the #arrow channel but I’ll give it a go 😛
Pair
is a tuple with no info about what
first
and
second
mean. See “tuples obfuscate” in the Google Guava ideas graveyard: https://github.com/google/guava/wiki/IdeaGraveyard
Either
is an abstraction built for modelling the common scenario where an operation can succeed or fail, with failure represented by
left
and success represented by
right
. This can be an alternative to declaring functions that have a return type for success but rely on throwing exceptions to indicate failure.
Either
provides functions like
fold
,
flatMap
etc. that allow chaining operations, and even a comprehension that allows writing imperative code in a block without using
flatMap
etc. TL;DR
Pair
is very broad and
Either
is suited to a specific use case
e
Pair always contains two values (one of each type), Either always contains one value (of either type). in PL/type theory terms, Pair<A, B> is A×B and Either<A, B> is A+B, which intuitively maps to the number of values they can take on
l
that’s clear, thank you. Pair is product type, Either is Union.
e
to be pedantic, it could be called a tagged union, since
Either<A, A>
contains more information than
A | A
. and Arrow's functions are very targeted towards using
Either
as an error-handling monad. but yes, exactly