I was wondering if it would be possible to use Scala macros to generate something equivalent to the following:
sealed type Foo
type Bar <: Foo
type Baz <: Foo
Then the following expression would be recognized as not being exhaustive
(foo: Foo) match {
case bar: Bar => ???
}
Looking at the comments in PatternMatching.scala
, it looks like there could be a way to communicate those constraints to the typechecker.
type Bar <: Foo
defines an abstract type member, it would be perfectly legal for it to be given the concrete definition type Bar = Foo
, in which case the match would be exhaustive. The compiler would need the code for the match and the code giving the concrete definition to Bar
to compare to decide if the match was exhaustive. Because these could be in separate classes/traits, compiled separately from each other, this is not possible. Suppose a superclass has the abstract definition and is compiled, then a subclass with the concrete definition is compiled, then the match statement is added in the superclass and compiled. The compiler has no idea that the subclass exists when compiling the new definition of the superclass, so it has no way of knowing that the match isn't exhaustive.