aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features199/java18/SwitchPatternPreview2Error2.java
diff options
context:
space:
mode:
Diffstat (limited to 'tests/features199/java18/SwitchPatternPreview2Error2.java')
-rw-r--r--tests/features199/java18/SwitchPatternPreview2Error2.java21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/features199/java18/SwitchPatternPreview2Error2.java b/tests/features199/java18/SwitchPatternPreview2Error2.java
new file mode 100644
index 000000000..3cee435e0
--- /dev/null
+++ b/tests/features199/java18/SwitchPatternPreview2Error2.java
@@ -0,0 +1,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);
+ }
+ }
+}