Using qualified enum in `old` switch

Jan Lahoda jan.lahoda at oracle.com
Mon Jul 10 15:31:43 UTC 2023


Hi Mikhail,


Oops, filed:

https://bugs.openjdk.org/browse/JDK-8311825


Thanks for the report!


Jan


On 10. 07. 23 10:20, Mikhail Pyltsin wrote:
>   Hi!
> I am investigating a new version of jep440+441
> (https://cr.openjdk.org/~gbierman/jep440%2B441/jep440+441-20230612/specs/patterns-switch-record-patterns-jls.html)
> After `2023-06-12: Misc editorial changes.` I can't find any explicit 
> mention that it is allowed to use qualified names for enums in `old` 
> switch.
> But according to https://openjdk.org/jeps/441 it must be allowed
> ```
> |static void goodEnumSwitch2(Coin c) { switch (c) { case HEADS -> { 
> System.out.println("Heads"); } case Coin.TAILS -> { // Unnecessary 
> qualification but allowed System.out.println("Tails"); } } }|
>
> ```
> before  `2023-06-12: Misc editorial changes.`, It was
> ```Every|case|constant must be either (1) the|null|literal, (2) a 
> constant expression (15.29 
> <https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.29>), 
> or (3) the (simple or qualified)name of an enum constant (8.9.1 
> <https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.9.1>); 
> otherwise a compile-time error occurs. A single|null|case constant may 
> also be paired with the|default|keyword.
> ```,  but now there is no mention of the type of enum names.
>
> Could you help me, which point allows it now?
>
> This question arose because the next code doesn't produce errors:
> ```
>     enum EN{A, B}
>
>     public void test(EN en) {
>         switch (en) {
>             case A -> System.out.println("a");
>             case EN.A -> System.out.println("a too");
>             case EN.B -> System.out.println("b");
>         }
>     }
> ```
> Is this expected?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230710/2d2af1f2/attachment-0001.htm>


More information about the compiler-dev mailing list