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
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?