RFR: 8273328: Compiler implementation for Pattern Matching for switch (Second Preview)
Vicente Romero
vromero at openjdk.java.net
Tue Nov 2 19:36:08 UTC 2021
On Tue, 2 Nov 2021 10:51:58 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> The specification for pattern matching for switch (preview)[1] has been updated with two changes:
> -any type of pattern (including guarded patterns) dominates constant cases. Dominance across pattern/non-constant cases is unchanged. (See the change in `Attr.java`.)
> -for sealed hierarchies, it may happen some of the subtypes are impossible (not castable) to the selector type. Like, for example:
>
> sealed interface I<T> {}
> final class A implements I<String> {}
> final class B<T> implements I<T> {}
> ...
> I<Integer> i = ...;
> switch (i) {
> case A a -> {} //case not allowed as I<Integer> is not castable to A
> case B b -> {}
> }
>
>
> But, the exhaustiveness checks in JDK 17 required all the permitted subclasses, including `A`, are covered by the switch, which forced a `default` clause in place of `case A a` for cases like this. The updated specification excludes such impossible permitted subclasses from exhaustiveness checks, so the default is no longer needed and this:
>
>
> I<Integer> i = ...;
> switch (i) {
> case B b -> {}
> }
>
>
> compiles OK.
> (See the change in `Flow.java`.)
>
> [1] http://cr.openjdk.java.net/~gbierman/jep420/jep420-20211020/specs/patterns-switch-jls.html
looks good to me
-------------
Marked as reviewed by vromero (Reviewer).
PR: https://git.openjdk.java.net/jdk/pull/6209
More information about the compiler-dev
mailing list