Or to understand the different behavior in 2.9 and 2.10
Scala 2.10 seems to have updated for comprehension for Libo. At 2.10:
scala> val a = Right(5)
a: scala.util.Right[Nothing,Int] = Right(5)
scala> for {aa: Int <- a.right} yield {aa}
<console>:9: error: type mismatch;
found : Int => Int
required: scala.util.Either[Nothing,Int] => ?
for {aa: Int <- a.right} yield {aa}
^
In paragraph 2.9.3 above, everything is fine.
scala> val a = Right(5)
a: Right[Nothing,Int] = Right(5)
scala> for {aa: Int <- a.right} yield {aa}
res0: Product with Serializable with Either[Nothing,Int] = Right(5)
Very easy to fix by simply removing the type for aa in 2.10. But I am wondering why the behavior changes as such between 2.9 and 2.10.
source to share
This is due to the fact that you open a bug that adds withFilter
even if it is not required. To explain this, a short introduction:
In the 2.10
compilation below c -Xprint:typer
is given
for {aa <- a.right} yield {aa}
Temp.this.a.right.map[Int](((aa: Int) => aa))
//for below
for {aa:Int <- a.right} yield {aa}
a.right.filter[Nothing](((check$ifrefutable$1: Int) => (check$ifrefutable$1: Int @unchecked) match {
case (aa @ (_: Int)) => true
case _ => false
})).map[B](((aa: Int) => aa))
This is obviously a mistake in the second case. Because in short you are doing Option[scala.util.Either[Nothing,Int]].map((aa:Int) => aa)
, which is a mistake, since it expects the card over Either
and not Int
.
In Scala 2.9.3 , for both of the above cases, it gives:
a.right.map[Int](((aa: Int) => aa))
The 2.10
added sentence with-filter
. From the spec for understanding :
The translation scheme is as follows. At the first stage, each generator p <- e, where p is not irrefutable for type e is replaced by
p <- e.withFilter {case p => true; case _ => false}
In your first case p
Irrefutable (Point-1 in spec) so no withFilter is added. In your second case p
Irrefutable (dot-2, so withFilter
shouldn't add. Error.
Similar reading: Why should a filter be defined for pattern matching in a for loop in scala?
source to share