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.

SwitchPatternPreview2Error.java 1.2KB

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * Inspired by examples in https://openjdk.java.net/jeps/420
  3. */
  4. public class SwitchPatternPreview2Error {
  5. static void constantLabelMustAppearBeforePattern1(Object o) {
  6. switch (o) {
  7. case Integer i && i > 0 -> System.out.println("positive integer: " + o);
  8. case -1, 1 -> System.out.println("special case:" + o);
  9. case Integer i -> System.out.println("other integer: " + o);
  10. default -> System.out.println("non-integer: " + o);
  11. }
  12. }
  13. static void constantLabelMustAppearBeforePattern2(Object o) {
  14. switch (o) {
  15. case -1, 1 -> System.out.println("special case:" + o);
  16. case Integer i -> System.out.println("other integer: " + o);
  17. case Integer i && i > 0 -> System.out.println("positive integer: " + o);
  18. default -> System.out.println("non-integer: " + o);
  19. }
  20. }
  21. static void constantLabelMustAppearBeforePattern3(Object o) {
  22. switch (o) {
  23. case Integer i && i > 0 -> System.out.println("positive integer: " + o);
  24. case Integer i -> System.out.println("other integer: " + o);
  25. case -1, 1 -> System.out.println("special case:" + o);
  26. default -> System.out.println("non-integer: " + o);
  27. }
  28. }
  29. }