I’m attempting to pattern match on a Seq to detect sequences containing at least one element (yes, there are other ways to check this, but…) and the compiler is reporting that a “match may not be exhaustive”.
I just wanted to sanity check here before thinking about a compiler bug report.
I started with just two cases:
seq match {
case Seq() => println(s"$seq is empty")
case _ +: _ => println(s"$seq has one or more elements")
}
(equivalently case Seq(_, _ @_*)
exhibits the same problem)
This resulted in the compiler warning that:
match may not be exhaustive.
It would fail on the following input: (x: Seq[?] forSome x not in Nil)
def test(seq: Seq[_]): Unit = seq match {
… which seems incorrect to me. Indeed adding an additional case for an empty list does not help:
seq match {
case Seq() => println(s"$seq is empty")
case Seq(_) => println(s"$seq has one element")
case _ +: _ => println(s"$seq has one or more elements")
}
… and the compiler wants a case _
before it is satisfied that the match is exhaustive, but this case can never be hit, which is easily verified at runtime.
The equivalent matches using classic List cons patterns doesn’t have this problem.
Am I missing something obvious here, or is this just a blindspot in the compiler?