Using Scala 2.9.1, consider the following two examples of Either :
scala> val er: Either[String, Int] = Right(1) er: Either[String,Int] = Right(1) scala> val el: Either[String, Int] = Left("a") el: Either[String,Int] = Left(a)
Itβs good that, using the left and right projections, you can use for understanding (through the biased monad of the projected Either):
scala> for { r <- er.right } yield r * 2 res6: Product with Either[String,Int] with Serializable = Right(2) scala> for { r <- el.right } yield r * 2 res7: Product with Either[String,Int] with Serializable = Left(a)
Can someone explain to me why it was decided not to return the filter method? I expected the following to work:
scala> for { r <- er.right if r > 2 } yield r * 2 // r is NOT greater than 2! res8: Product with Either[String,Int] with Serializable = Left(a)
Instead, you get the following error:: 9: error: the value * is not a member Either [Nothing, Int] for {r <er.right if r> 2} gives r * 2
It seems that the main call to Either.RightProjection#filter really returns Option :
scala> er.right.filter(_ > 2) res9: Option[Either[Nothing,Int]] = None
This defeats the use of the if clause in understanding, at least, how I tried to use it.
Does anyone have an explanation why this design is the way it is?