That’s right. We made a decision to simplify the grammar of switch labels for 
the JEP to only have a single pattern. However, if you use the "colon form” you 
can express similar code:

gmb@gmb-mac src % cat Switch.java
public class Switch {

    sealed interface X { }

    enum E implements X {A;}

    record R() implements X { }

    public void work(X x) {
        var result = switch (x) {
            case E.A:
            case R(): yield 42;
                // exhaustive!
        };
    }

    public static void main(String[] args) {
        System.out.println("complete");
    }
}
gmb@gmb-mac src % java Switch.java
complete


As you point out, a more uniform treatment would treat all constants as 
patterns and allow them all to appear in pattern lists of record patterns, for 
example. Working on it!!

Gavin

On 26 Sep 2023, at 14:52, Brian Goetz <[email protected]> wrote:

I see now I answered a slightly different question :)

We do support case labels for enums in pattern switches, and they participate 
in exhaustiveness.  But we don't currently support mixing `case pattern, 
constant-label` in a single case label.  This is waiting for a more 
comprehensive treatment of constants as patterns.

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 
<[email protected]><mailto:[email protected]> 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



Reply via email to