Right case class only captures the right-side of
Either[+A, +B] disjunction. So it makes sense to me that the type parameter that it holds are limited only to
map shows the practical problem with carrying both side’s type information needlessly.
In the example,
this is typed to
Either[A, B]. Even in the case for
l@Left(_), the type is
Either[A, B]. The right-biased
map would then transform the right-side, and thus change the return type to
Either[A, B1]. So
l@Left(_) gets a type error saying found
Either[A, B] when
Either[A, B1] is expected.
We would eventually want to treat
Either[+A, +B], so we need a placeholder type. Using the bottom type
Nothing gives us the maximum flexibility since:
Nothing is a subtype of ever other type, and
Either[+A, +B] is covariant on both sides,
so a value of
Either[Nothing, B] can reside on an arbitrary
Either[?, B]. Woot.
We have precedence for using the bottom type this way:
case object Nil extends List[Nothing]
case object None extends Option[Nothing]
The “empty” values of
Option are both parameterized with
If in the source code you have
Right[Foo, Bar], this change breaks the source compatibility.
I think it’s a minor problem since you can just use
Either[Nothing, Bar] or
Either[Foo, Bar] as the type.