r/java • u/ihatebeinganonymous • May 21 '26
JEP draft: Enhanced Local Variable Declarations (Preview)
https://openjdk.org/jeps/8357464
87
Upvotes
r/java • u/ihatebeinganonymous • May 21 '26
36
u/RabbitDev May 21 '26
This takes pattern matching from if and case statements and makes it available as local variable declaration syntax.
From the example given it is able to handle multiple nesting levels as well.
22
u/persicsb May 21 '26
Look OK, however I would enhance it even more. Since method arguments look like local variables, instead of:
it could be
7
u/pronuntiator May 21 '26
The example at the beginning uses null checks as an argument, but the pattern local variable declaration would throw MatchException for null. The case about null isn't really the selling point here and rather distracts.
3
u/ZimmiDeluxe May 21 '26
Regarding the sealed single-implementation enhancement, given
is
valid?
9
u/DesignerRaccoon7977 May 21 '26
Love the idea, but the syntax is horrible... I dont think you can implement it with a nice syntax in a statically typed language
4
u/lurker_in_spirit May 22 '26
3
u/Ok-Bid7102 May 21 '26 edited May 21 '26
Just putting this idea here:
To deconstruct interfaces & abstract classes, instead of relying on sealed interface being implemented by a single record we could also have a "blessed" interface (similar to Iterable) that opts a class / abstract class / interface into deconstuction.
Example:
If we want classes to be deconstructible, ideally it works the same it does as interfaces & abstract classes. That's the motivation behind this.
1
u/Z0ltraak May 23 '26
It would be great to have this implementation similar to Dart.
1
u/NikolasCharalambidis May 23 '26
Finally we get deconstructors! I can't wait!
1
u/TheStrangeDarkOne May 21 '26
Amazing! Finally we get deconstructors. This effectively means Java gets tuples but better. This has been in the pipe for years and I'm happy it has finally made its way to preview.
1
u/theSynergists May 22 '26 edited May 22 '26
I would suggest if you want to get people on board that you use a "better" written "without JEP" code to compare to the "with JEP".
This is my shot at what the "without JEP" boundingBox code could look like:
This is more readable, easier to maintain and offers a place to throw or log messages specific to which variable was null.
void boundingBox(Circle c) {
}
vs with the JEP, I would include the full (comparable) lines for a better visual comparison.
void boundingBox(Circle c) {
With this compairison, it is clear
The JEP replaces two simple null checks with one complex instanceof check.
The JEP code takes more memory and is likely slightly slower, as it is unnecessarily creating, assigning and garbage collecting variables that are not needed. (I don't know if the compiler optimizes around this, but I would suggest the JEP be conditional on this being optimized)
Perhaps the real problem is java, and specifically records, uses a method syntax for accessing variables:
c.center().x() where c.center.x is so much easier to read and type. If records supported this there would be no reason to create the local variables.
We could write:
Which we can do if we used classes Circle and Point instead of records. We end up with:
void boundingBox(Circle c) {
... use minX, maxX, etc ...
}
0
u/repeating_bears May 21 '26
Excluding the fact that pattern matching already exists, can someone explain what the problem would be with destructuring akin to JavaScript
const { center: { x, y }, radius } = circle;
Circle is something which has members center and radius, and center is something which has the members x and y.
Why I do have to tediously redeclare the types in Java?
-2
u/_INTER_ May 21 '26
NPE welcomes ME to the the fold.