forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatchingForSwitchFourthPreviewTest.java
More file actions
39 lines (32 loc) · 1.06 KB
/
PatternMatchingForSwitchFourthPreviewTest.java
File metadata and controls
39 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* To run: `java --enable-preview --source 20 PatternMatchingForSwitchFourthPreviewTest.java`
*/
public class PatternMatchingForSwitchFourthPreviewTest {
public static void main(String[] args) {
recordErrorInSwitchPatternMatching();
genericRecordInSwitch();
}
static void recordErrorInSwitchPatternMatching() {
var dot = new OneDimensionalPoint(10);
switch (dot) {
// will cause MatchException with wrapped exception (the record pattern completes abruptly with the ArithmeticException)
case OneDimensionalPoint(var x): System.out.println("1D point");
// the occurring in guarded clause, it just rethrows the exception
// will cause ArithmeticException
// case OneDimensionalPoint p when (p / 0 == 1): System.out.println("Non sense");
}
}
static void genericRecordInSwitch() {
var w = new Wrapper<String>("some text");
switch (w) {
// will infer Wrapper<String>
case Wrapper(var v): System.out.println("Wrapped value: " + v);
}
}
}
record OneDimensionalPoint(int x) {
public int x() {
return x / 0;
}
}
record Wrapper<T>(T t) {}