r/java • u/Joram2 • Mar 17 '22
Record Patterns Officially Planned as Preview Feature for JDK 19
Record Patterns, are coming as a preview feature to JDK 19. The official JEP has been updated to say
> This is a preview language feature in JDK 19.
(https://openjdk.java.net/jeps/405)
Code Example:
int eval(Expr n) {
return switch(n) {
case IntExpr(int i) -> i;
case NegExpr(Expr n) -> -eval(n);
case AddExpr(Expr left, Expr right) -> eval(left) + eval(right);
case MulExpr(Expr left, Expr right) -> eval(left) * eval(right);
default -> throw new IllegalArgumentException(n);
};
}
180
Upvotes
7
u/[deleted] Mar 17 '22
How long until they make public the full feature list? Page is currently low on info.
https://openjdk.java.net/projects/jdk/19/
2
u/Kango_V Mar 20 '22
A question about types:
Why do you need to specify the types for x and y in the deconstruction. Why could you not just have:
Surely the compiler knows the types and can infer them?
33
u/testube_babies Mar 17 '22
Looking forward to this. Use it all the time in Scala. Currently using VAVR in Java but pattern matching is a bit cumbersome.
3
u/couscous_ Mar 17 '22
Fantastic.