Reputation: 2717
Why does this code not type check?
def foo: Either[String, List[Int]] = {
val x = null: Either[String, String]
x match {
case l @ Left(_) => l
case Right(_) => Right(List(3))
}
}
Specifically, why can't/doesn't the compiler reify the type of Left[A,X] and Either[A,B]?
This happened in scala 2.8.2 and scala 2.9.2
Upvotes: 4
Views: 355
Reputation: 167901
There is a genuine conflict here:
Left[String, String] <: Either[String, List[Int]]
is not true. Left
is typed on both left and right parameters, and even though there is no actual difference between the two (so casting would be safe), this is not what you have told the compiler. So, of course, it will complain.
There are other weaknesses in type inference in pattern matching, but this is not one of them.
Edit: one can imagine an alternate singly-parameterized implementation of Left
in which
Left[String] <: Either[String, Nothing] <: Either[String, List[Int]]
which would throw away type information. Methods like swap
would be tricker to use, but it would allow the pattern given by the OP. However, the type-preserving form was used in the standard implementation in Scala.
Upvotes: 9