"Represents a value of one of two possible types (a disjoint union.) Instances of Either are either an instance of Left or Right.A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage, scala.None is replaced with a Left which can contain useful information. Right takes the place of Some. Convention dictates that Left is used for failure and Right is used for success."
-1
The Unbiased Either is perfect for representing one of two values -
either the right or the left - without implying that one of them is
more "correct" than the other.
If there is bias towards right or left there is usually a good name
for that bias. For example Validation from Scalaz, and ParseResult
from the parser combinators. They are often fixed in one of the
parameters too, like Future.
That said, I do like the idea of a general right biased either, but I
really really don't want to sacrifice the unbiased one in in the
process.
"You should really use Maybe, not Either. I know all the books in print right now don't even mention it yet, but you'll just have to trust me until you come to understand for-comprehensions better, and monads"...Followed by the inevitable web search for monads and (more often than not) the resulting confusion followed by claims of Scala being too complicated and "academic". Then followed by the further inevitable repair of any damage so caused.
-1
I prefer unbiased Either. If we wanted to provide an implicit conversion to Option (via right projection) I doubt I'd mind, but Either can be used (and I do use it) to store two alternative things without bias. Adding bias will create a strong pressure for asymmetric code even though the conditions are symmetric.
If there is not an implicit conversion, I'd prefer a third class that was biased. If we wanted to avoid stepping on the Scalaz names, "Excused" might work.
--Rex
On Mon, May 14, 2012 at 3:08 AM, Jason Zaugg <jza...@gmail.com> wrote:(A follow-up from ticket SI-5793. [1])
Apologies in advance to any left-handers out there.
I propose (not that I'm the first) to right-bias Either. Concretely,
this means we would add map/flatMap etc directly to Either, rather
than going through RightProjection. RightProjection could probably
deprecated.
Is there any previous discussion on this? Any arguments against?
-jason
[1] https://issues.scala-lang.org/browse/SI-5793
Right-bias is not about being able to quickly convert to an Option, it's about being able to chain a sequence of operations, each of which takes the right output of the previous operation and yields an either.
Most especially, it's about being able to do so in the context of a for-comprehension.
Most especially, it's about being able to do so in the context of a for-comprehension.
Fair enough. But if this is the only thing it can do, I'm not sure it's worth it.
We introduced `Try` a while back along with SIP-14 (Futures). `Try` is analogous to `Either` with the exception that it's success-biased. That is, Either is symmetric, it has two type parameters that are wrapped (symmetrically) in either `Left` or `Right`. In general, by convention, `Either` can be used to represent a "successful" or a "failed" result, by representing some generic type in its right projection, and a subtype of Throwable in its left projection. However, this is by convention only, one can of course swap these projections (whether accidentally or purposefully) or one can represent two successful values in each projection if they so desire.`Try`, on the other hand, is parameterized only on one type, with this success/failure use case in mind. That is, the successful case is wrapped in `Success` (parameterized on T) while the failed case is a `Throwable` wrapped in `Failure`.`Try` comes from Twitter, who use it frequently in their codebase (see twitter/util on github), and who have found that it results in more readable and less error-prone code for this success/failure use case of `Either`.
How about doing the following:
* leave Either as it is, since the name doesn't imply any bias
(although Right certainly does), and it looks like changing it would
only buy you definitions in for-comprehensions (and not having to add
.right)
* introduce a completely new class, Checked, having OK and KO as the
type constructors
* include support for combining Checked values in both a fail-fast and
fail-slow manner, including applicative-functor support (<*>
operator), similar to that which I describe here:
http://robsscala.blogspot.co.uk/2012/05/validating-multiple-values-at-once.html
scala> implicit def doTheRightThing[A,B](x: Either[A,B]): Either.RightProjection[A,B] = x.rightdoTheRightThing: [A, B](x: Either[A,B])Either.RightProjection[A,B]scala> val one: Either[Int,String] = Right("Bippy")one: Either[Int,String] = Right(Bippy)scala> val two: Either[Int,String] = Left(42)two: Either[Int,String] = Left(42)scala> one map {_.toUpperCase}res0: Product with Either[Int,java.lang.String] with Serializable = Right(BIPPY)
scala> two map {_.toUpperCase}res1: Product with Either[Int,java.lang.String] with Serializable = Left(42)