Skip to content

Commit 6533d39

Browse files
committed
JDK 20: added example of Record pattern
1 parent e587a28 commit 6533d39

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

java-19/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ To run each example use: `java --enable-preview --source 19 <FileName.java>`
1515
* Record patterns
1616
* added suport to deconstruct record values in pattern matcher
1717
* record pattern: `Point(int x, int y)`
18-
* the variable `int x` is initialized with the result of accessor method `Point.x()`, not directly from the field `x`
18+
* the variable `int x` is initialized with the result of accessor method `Point.x()`, not directly from the field `x`
19+
* the variable doesn't need to be the same as the name of the record component
20+
* the null value does not match any record pattern
1921
* now we can use type pattern and record pattern together
2022
* we can check the type and extract the record components using `instanceof` operator
2123
* `o instanceOf Point(int x, int y)`

java-20/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ To run each example use: `java --enable-preview --source 20 <FileName.java>`
44

55
## Features
66

7-
Exploring... =)
7+
* Record patterns (second preview)
8+
* added support for inference of type arguments of generic record patterns;
9+
* now it generic type can be inferred
10+
* given `record Decorator<T>(T t) {}` and variable `Decorator<Decorator<String>> wr`, thee record pattern generic type can be inferred in `w insteaceof Decorator(Decorator(var s))`
11+
* added support for record patterns to appear in the headere of an enhanced for statement;
12+
* `for (Point(var x, var y) : shapePoints)`
13+
* remove support for named record pattner.
14+
15+
## JEPs
16+
17+
* [432](https://openjdk.java.net/jeps/432) - Record Patterns (Second Preview)
18+
* [433](https://openjdk.java.net/jeps/433) - Pattern Matching for switch (Fourth Preview)
819

920
## Links
1021

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* To run: `java --enable-preview --source 20 RecordsPatternSecondPreviewTest.java`
3+
*/
4+
public class RecordPatternsSecondPreviewTest {
5+
public static void main(String[] args) {
6+
enhancedForLoop();
7+
}
8+
9+
public static void enhancedForLoop() {
10+
var points = new Point[] {
11+
new Point(10, 10),
12+
new Point(20, 20),
13+
new Point(30, 30),
14+
new Point(20, 50),
15+
new Point(10, 60)
16+
};
17+
18+
// we can now deconstruct a record type in the enhanced for loop
19+
for (Point(int x, int y) : points) {
20+
System.out.print("Drawing at x=%d and y=%d", x, y);
21+
}
22+
}
23+
24+
public static void genericInferrenceTest() {
25+
var point = new Point(42, 42);
26+
var decoratedPoint = new Decorator(new ColoredPoint(point, "RED"));
27+
var anotherDecorated = new Decorator(decoratedPoint);
28+
29+
// here we don't need to use `Decorator<Decorator<ColoredPoint>>(Decorator<ColoredPoint>(ColoredPoint cp))` like in JDK 19
30+
if (anotherDecorated instanceof Decorator(Decorator(ColoredPoint(Point(int x, int y), String color)))) {
31+
System.out.println("Aren't you using too much decorator?");
32+
System.out.printf("x=%d, y=%d; color=%s", x, y, color);
33+
}
34+
}
35+
36+
static void invalidGenericEnhancedForLoop() {
37+
var items = new Decorator<Object>[] { new Object() };
38+
39+
// the pattern is not exhaustive (should be Decorator<Object>)
40+
// for (Decorator<String> d : items) {
41+
// }
42+
}
43+
}
44+
45+
record Point(int x, int y) {}
46+
47+
record ColoredPoint(Point p, String color) {}
48+
49+
record Decorator<T>(T t) {}
50+

0 commit comments

Comments
 (0)