aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features199/java18/SwitchPatternPreview2Error2.java
blob: 3cee435e003055e08ae9fc8436b735cd2fff03a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Inspired by examples in https://openjdk.java.net/jeps/420
 */
public class SwitchPatternPreview2Error2 {
  static void constantLabelsMustAppearBeforePatterns2(Object o) {
    switch (o) {
      case null -> System.out.println("value unavailable: " + o);
      // This seems to be a bug in JEP 420 implementation. Those constants should be compatible with 'Object'.
      // case -1, 1 -> System.out.println("absolute value 1: " + o);
      // case "hello" -> System.out.println("string value: " + o);

      // 'Integer value' dominates the next two, more specific ones -> error
      case Integer value -> System.out.println("other integer: " + o);
      case Integer value && (value == 1 || value == -1) -> System.out.println("absolute value 1: " + o);
      case Integer value && value > 0 -> System.out.println("positive integer: " + o);

      case String value && value.startsWith("hello") -> System.out.println("greeting: " + o);
      default -> System.out.println("other type: " + o);
    }
  }
}