Closed srikanth-sankaran closed 3 weeks ago
14.30.3 Properties of Patterns
A pattern p is said to dominate another pattern q if every value that matches q also matches p, and is defined as follows: • A pattern p dominates a type pattern that declares a pattern variable of type T if p is unconditional for T. • A pattern p dominates a record pattern with type R if p is unconditional for R. • A record pattern with type R and component pattern list L dominates another record pattern with type S and component pattern list M if (i) R and S name the same record class, and (ii) every component pattern, if any, in L dominates the corresponding component pattern in M. • A pattern p dominates a match-all pattern with type T if p is unconditional for T.
A pattern p is said to be unconditional for a type T if it can be determined at compile time that every value of type T will match p, and so the run time, testing aspect of pattern matching could be elided. It is defined as follows: • A type pattern that declares a pattern variable of a reference type S is unconditional for a reference type T if the erasure of T is a subtype of the erasure of S. • A type pattern that declares a pattern variable of a primitive type P is unconditional for the type P. • A match-all pattern is unconditional for every type T. Note that no record pattern is unconditional because the null reference does not match any record pattern.
The following program is accepted by ECJ but rejected by javac with
X.java:18: error: this case label is dominated by a preceding case label
Indeed running the ECJ compiled program produces:
showing the domination.