The following code defines an exhaustive match over a Vector
, v
.
def testExhaustiveness(v: Vector[Int]) = {
v match {
case Vector() => println("v is empty")
case ns :+ n => println("v has at least one element")
}
}
@main
def main = {
testExhaustiveness(Vector()) // v is empty
testExhaustiveness(Vector(1)) // v has at least one element
testExhaustiveness(Vector(1, 2)) // v has at least one element
}
However, the compiler gives [E029]
18 | v match {
| ^
|match may not be exhaustive.
|
|It would fail on pattern case: Vector(_, _*), Vector(_, _*), Vector(_, _*), Vector(_, _*), Vector(_, _*), Vector(_, _*)
|
| longer explanation available when compiling with `-explain`
I imagine this is in error. So, how can I get the compiler to recognise my exhaustive match? I understand we can suppress warnings with a pragma, but is there a destructuring that is accepted by the compiler?
case _ => // empty case
. But, note that pattern matching onVector
is kind of a hack, rather than a default operation like onList
and in general it would be better if you write your code either usingList
or otherVector
operations.Vector
is not build likeList
, withsealed
+ 2 publiccase class
es. it has several private implementations and things like:+
or+:
are objects implementingunapply
method. Unfortunately, even if one could build an exhaustive match from a set of values withunapply
compiler does not know it, and so it should complain.