Why does Scala 2.10 give 'match may not be exhaustive' warning when matching on singleton types?
-
16-06-2021 - |
Question
In Scala 2.10.0-M4
object X
def f(e: Either[Int, X.type]) = e match {
case Left(i) => i
case Right(X) => 0
}
gives:
warning: match may not be exhaustive.
It would fail on the following input: Right(<not X>)
Is this correct? Surely the match is in fact exhaustive.
(Meanwhile, back in Scala 2.9.X we get
error: pattern type is incompatible with expected type;
found : object X
required: X.type
case Right(X) => 0
which presumably was a bug.)
Solution 2
And it's been patched, c.f. https://github.com/retronym/scala/compare/ticket/5968.
OTHER TIPS
Sadly, there are two values which inhabit X.type
. One is the obvious one, X
, and the other of course is null
. Thus your pattern has missed a case :(
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow