So, the scala compiler complains that pattern matching may not be complete for the method foo, and I wonder why. This is the code:
abstract class Foo {
def foo(that: Foo): Unit = (this, that) match {
case (Foo_1(), Foo_1()) =>
case (Foo_1(), Foo_2()) =>
case (Foo_2(), Foo_1()) =>
case (Foo_2(), Foo_2()) =>
}
def fooThis(): Unit = this match {
case Foo_1() =>
case Foo_2() =>
}
def fooThat(that: Foo): Unit = that match {
case Foo_1() =>
case Foo_2() =>
}
}
case class Foo_1() extends Foo
case class Foo_2() extends Foo
And this is a mistake:
Warning:(5, 32) match may not be exhaustive.
It would fail on the following inputs: (Foo(), _), (Foo_1(), _), (Foo_2(), _), (_, Foo()), (_, Foo_1()), (_, Foo_2()), (_, _)
def foo(that: Foo): Unit = (this, that) match {
Because thisand thathave a type foo, and foocan only be of a type Foo_1or Foo_2, in cases foo- all possible combinations.
I added fooThis, and fooThatfor completeness and showed that the coincidence Foo_1and Foo_2enough. A compiler post suggests that there are other types that can be matched (i.e., fooand _).
So why is this warning shown?
on this topic:
, , , . fooThis
def fooThis(): Unit = (this, Foo_1()) match {
case (Foo_1(),_) =>
case (Foo_2(),_) =>
}
Warning:(13, 27) match may not be exhaustive.
It would fail on the following input: (_, _)
def fooThis(): Unit = (this, Foo_1()) match {