You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwitchPatternPreview3Error2.java 1019B

123456789101112131415161718192021
  1. /**
  2. * Inspired by examples in https://openjdk.java.net/jeps/420
  3. */
  4. public class SwitchPatternPreview3Error2 {
  5. static void constantLabelsMustAppearBeforePatterns2(Object o) {
  6. switch (o) {
  7. case null -> System.out.println("value unavailable: " + o);
  8. // This seems to be a bug in JEP 420 implementation. Those constants should be compatible with 'Object'.
  9. // case -1, 1 -> System.out.println("absolute value 1: " + o);
  10. // case "hello" -> System.out.println("string value: " + o);
  11. // 'Integer value' dominates the next two, more specific ones -> error
  12. case Integer value -> System.out.println("other integer: " + o);
  13. case Integer value when (value == 1 || value == -1) -> System.out.println("absolute value 1: " + o);
  14. case Integer value when value > 0 -> System.out.println("positive integer: " + o);
  15. case String value when value.startsWith("hello") -> System.out.println("greeting: " + o);
  16. default -> System.out.println("other type: " + o);
  17. }
  18. }
  19. }