The switch block of a switch expression or switch statement is exhaustive for a selector expression e if one of the following cases applies:
- There is a
defaultlabel associated with the switch block. - There is a
casenull,defaultlabel associated with the switch block. - The set containing all the
caseconstants andcasepatterns appearing in an unguardedcaselabel (collectively known ascaseelements) associated with the switch block is non-empty and covers the type of the selector expressione.
A set of case elements, P, covers a type T if one of the following cases applies:
-
P covers a type U where T and U have the same erasure.
-
P contains a pattern that is unconditional for T.
-
T is a type variable with upper bound B and P covers B.
-
T is an intersection type T_1
&...&T_n and P covers T_i, for one of the types T_i (1≤ i ≤ n). -
The type T is an enum class type E and P contains all of the names of the enum constants of E.
A
defaultlabel is permitted, but not required, in the case where the names of all the enum constants appear ascaseconstants. For example:enum E { F, G, H } static int testEnumExhaustive(E e) { return switch(e) { case F -> 0; case G -> 1; case H -> 2; // No default required! }; } -
The type T names an
abstractsealedclass orsealedinterface C and for every permitted direct subclass or subinterface D of C, one of the following two conditions holds:- There is no type that both names D and is a subtype of T, or
- There is a type U that both names D and is a subtype of T, and P covers U.
A
defaultlabel is permitted, but not required, in the case where the switch block exhausts all the permitted direct subclasses and subinterfaces of anabstractsealedclass orsealedinterface. For example:sealed interface I permits A, B, C {} final class A implements I {} final class B implements I {} record C(int j) implements I {} // Implicitly final static int testExhaustive1(I i) { return switch(i) { case A a -> 0; case B b -> 1; case C c -> 2; // No default required! }; }As the switch block contains
casepatterns that match against all values of typesA,BandC, and no other instances of typeIare permitted, this switch block is exhaustive.The fact that a permitted direct subclass or subinterface may only extend a particular parameterization of a generic
sealedsuperclass or superinterface means that it may not always need to be considered when determining whether a switch block is exhaustive. For example:sealed interface J<X> permits D, E {} final class D<Y> implements J<String> {} final class E<X> implements J<X> {} static int testExhaustive2(J<Integer> ji) { return switch(ji) { // Exhaustive! case E<Integer> e -> 42; }; }As the selector expression has type
J<Integer> the permitted direct subclassDneed not be considered as there is no possibility that the value ofjican be an instance ofD. -
The type T names a record class R, and P contains a record pattern
pwith a type that names R and for every record component of R of type U, if any, the singleton set containing the corresponding component pattern ofpcovers U.A record pattern whose component patterns all cover the type of the corresponding record component is considered to cover the record type. For example:
record Test<X>(Object o, X x){} static int testExhaustiveRecordPattern(Test<String> r) { return switch(r) { // Exhaustive! case Test<String>(Object o, String s) -> 0; }; } -
P rewrites to a set Q and Q covers T.
A set of case elements, P, rewrites to the set Q, if a subset of P reduces to a pattern
p, and Q consists of the remaining elements of P along with the patternp.A non-empty set of patterns, RP, reduces to a single pattern rp if one of the following holds:
-
RP covers some type U, and rp is a type pattern of type U.
-
RP consists of record patterns whose types all erase to the same record class R with k (k≥1) components and there is a distinguished component
c_r(1≤r≤k) of R such that for every other componentc_i(1≤i≤k, i≠r) the set containing the component patterns from the record patterns corresponding to componentc_iis equivalent to a single patternq_i, the set containing the component patterns from the record patterns corresponding to the componentc_rreduces to a single patternq, and rp is the record pattern of type R with a pattern list consisting of the patternsq_1, ...,q_{r-1},q,q_{r+1}, ...,q_k.A non-empty set of patterns EP is equivalent to a single pattern ep if one of the following holds:
- EP consists of type patterns whose types all have the same erasure T, and ep is a type pattern of type T.
- EP consists of record patterns whose types all erase to the same record class R with k (k≥1) components and for every record component the set containing the corresponding component patterns from the record patterns is equivalent to a single pattern
q_j(1≤j≤k), and ep is the record pattern of type R with a component pattern list consisting of the component patternsq_1,...q_k.
-
Ordinarily record patterns match only a subset of the values of the record type. However, a number of record patterns in a switch block can combine to actually match all of the values of the record type. For example:
sealed interface I permits A, B, C {}
final class A implements I {}
final class B implements I {}
record C(int j) implements I {} // Implicitly final
record Box(I i) {}
int testExhaustiveRecordPatterns(Box b) {
return switch (b) { // Exhaustive!
case Box(A a) -> 0;
case Box(B b) -> 1;
case Box(C c) -> 2;
};
}
Determining whether this switch block is exhaustive requires the analysis of the combination of the record patterns. The set containing the record pattern Box(I i) covers the type Box, and so the set containing the patterns Box(A a), Box(B b), and Box(C c) can be rewritten to the set containing the pattern Box(I i). This is because the set containing the patterns A a, B b, C c reduces to the pattern I i (because the same set covers the type I), and thus the set containing the patterns Box(A a), Box(B b), Box(C c) reduces to the pattern Box(I i).
However, rewriting a set of record patterns is not always so simple. For example:
record IPair(I i, I j){}
int testNonExhaustiveRecordPatterns(IPair p) {
return switch (p) { // Not Exhaustive!
case IPair(A a, A a) -> 0;
case IPair(B b, B b) -> 1;
case IPair(C c, C c) -> 2;
};
}
It is tempting to apply the logic from the previous example to rewrite the set containing the patterns IPair(A a, A a), IPair(B b, B b), IPair(C c, C c) to the set containing the pattern IPair(I i, I j), and hence conclude that the switch block exhausts the type IPair. But this is incorrect as, for example, the switch block does not actually have a label that matches an IPair value whose first component is an A value, and second component is a B value. It is only valid to combine record patterns on one component if they match the same values in the other components. For example, the set containing the three record patterns IPair(A a, I i), IPair(B b, I i), and IPair(C c, I i) can be reduced to the pattern IPair(I j, I i).
A switch statement or expression is exhaustive if its switch block is exhaustive for the selector expression.