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

Mike Rettig mike.rettig at gmail.com
Fri May 21 14:09:31 UTC 2021


On Fri, May 21, 2021 at 8:44 AM <forax at univ-mlv.fr> wrote:

> 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
>

Thanks for the clarification. So enums and sealed types behave differently
with switch?

This compiles just fine with jdk 16.

enum Shape {
        Circle, Rectangle
    }

        Shape s = ...;
        switch (s){
            case Circle -> {
                System.out.println("Circle");
            }
        }

It's not consistent, but I guess it's better than the alternatives. Sealed
types without exhaustiveness aren't very useful.

Mike


More information about the jdk-dev mailing list