New candidate JEP: 456: Unnamed Variables and Patterns

Brian Goetz brian.goetz at oracle.com
Tue Sep 26 13:16:20 UTC 2023


I am pretty sure I saw some spec language for this go by, but Gavin can 
fill in the details.  But yes, this is desirable because it should be 
possible to mix enums and records under a sealed type:

     sealed interface X { }
     enum E implements X { A; }
     record R(int x) implements X { }

     ...

     switch (x) {
         case E.A: ...
         case R(int x): ...
         // exhaustive!
     }

There is some spec trickiness because switches on enum selectors _must_ 
use unqualified names historically, and this has to be rationalized.

On 9/26/2023 8:32 AM, Tagir Valeev wrote:
> Hello! As we are finalizing this feature, can we squeeze in a little
> improvement? Namely, support enum and patterns within the same case
> label, provided that the patterns do not declare any variables. Like:
>
> enum X {A, B}
>
> static void test(Object obj) {
>    switch (obj) {
>      case String _, X.B -> System.out.println("B or String");
>      default -> System.out.println("other");
>    }
> }
>
> public static void main(String[] args) {
>    Test.test("ddd");
>    Test.test(X.B);
> }
>
> Currently, such a code is not supported. Or will it be considered in
> future JEPs?
>
> With best regards,
> Tagir Valeev.
>
> On Mon, Sep 25, 2023 at 6:25 PM Mark Reinhold<mark.reinhold at oracle.com>  wrote:
>> https://openjdk.org/jeps/456
>>
>>    Summary: Enhance the Java language with unnamed variables, which
>>    can be initialized but not used, and unnamed patterns, which match a
>>    record component without stating the component's name or type. Both are
>>    denoted by an underscore character, _.
>>
>> - Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230926/3f333990/attachment-0001.htm>


More information about the amber-dev mailing list