JEP proposed to target JDK 17: 406: Pattern Matching for switch (Preview)

forax at univ-mlv.fr forax at univ-mlv.fr
Fri May 21 13:44:13 UTC 2021


> De: "Mike Rettig" <mike.rettig at gmail.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "jdk-dev" <jdk-dev at openjdk.java.net>
> Envoyé: Vendredi 21 Mai 2021 15:22:37
> Objet: Re: JEP proposed to target JDK 17: 406: Pattern Matching for switch
> (Preview)

> On Fri, May 21, 2021 at 2:07 AM Remi Forax < [ mailto:forax at univ-mlv.fr |
> forax at univ-mlv.fr ] > wrote:

>> ----- Mail original -----
>> > De: "Mike Rettig" < [ mailto:mike.rettig at gmail.com | mike.rettig at gmail.com ] >
>> > Will non-expression switches be exhaustive? I know that this has been
>> > discussed but I wanted to confirm the behavior for jdk 17.

>>> [
>>> https://mail.openjdk.java.net/pipermail/amber-spec-experts/2021-April/002959.html
>>> |
>>> https://mail.openjdk.java.net/pipermail/amber-spec-experts/2021-April/002959.html
>> > ]

>> > Sealed classes will be far more useful with exhaustiveness.

>> yes,
>> see 15.28.1 of the current spec [1].
> 15.28.1 is specifically referring to switch expressions. I am asking about the
> non-expression form.
> Will this switch compile even though it is not exhaustive?

my bad, 
in the changelog: A switch statement that uses the new features of this JEP is now required to be complete (the same notion that all switch expressions require) 

The rule for completness is in 14.11.1 the three rules after "A switch block is complete for a selector expression if the following are true:" 

> abstract sealed class Shape {...}
> ... class Circle    extends Shape {...}
> ... class Rectangle extends Shape {...}
> Shape s = ....
> switch(s){
> case Circle c ->{handleCircle(c);}
> //no case for Rectangle or default
> }

It's a sealed type so this specific rule applies: 

If the type of the selector expression, T , names a sealed interface or a sealed class that is abstract then at least one of the following is true: 

    1. 

All of the permitted subtypes of T are covered by the switch block. 
Rectangle is a permitted subtype but there is no case for it, so the switch statement is not complete. 

Rémi 


More information about the jdk-dev mailing list