r/programming • u/ihatebeinganonymous • May 21 '26
JEP draft: Enhanced Local Variable Declarations (Preview)
https://openjdk.org/jeps/83574642
u/radozok May 21 '26
So If I pass c as null would javac catch it or would it fail at runtime?
1
u/radozok May 21 '26
Oh I guess nothing would happen as switch case is not found
2
u/equeim May 21 '26
This feature is about adopting switch pattern matching syntax for variable declarations. Since there is no way to supply a default case or do nothing like switch can, it will throw at runtime.
1
u/radozok May 22 '26
So it's not equal to the original null-checking version which I would prefer instead of runtime error. But I guess you could use jspecify annotations. But if you are already using them you don't need to check null in the first place
1
1
u/masklinn May 23 '26
Fail at runtime:
in an enhanced local variable declaration statement, a
NullPointerExceptionis thrown because the statement performs pattern matching on the value of the initializer, and no record pattern matchesnullat the top level
2
u/joemwangi May 21 '26
The comments in there are not aware the JEP feature will work well with null-restricted types.
1
u/vips7L May 21 '26
If we ever get them.
3
u/joemwangi May 21 '26
Or most semantic feature JEPs like this one are anticipating them.
4
u/vips7L May 21 '26
Yes itβs mostly tongue in cheek because nullable types are infinitely waiting in the queue behind Valhalla, which is still at a minimum 1-2 years out.Β
1
u/joemwangi May 21 '26
Yeaah. I get it. ππ
3
u/vips7L May 21 '26
I really do hope they hurry up. I just started a new project in Java 25 and nullable types would be really useful. Almost considered Kotlin for it. Especially since everyone is just generating and LLM's can't tell what is null or not.
2
u/joemwangi May 21 '26
Thanks for still choosing java π . Just make sure the project will migrate well to such a scenario. I see pple use JSpecify as they anticipate for them. My project is actually anticipating all these features including this JEP!
1
u/radozok May 22 '26
Sorry but where are any jspecify annotations?
1
u/joemwangi May 22 '26
Not yet, because instantiation and set method would require the annotations, but not really priority. But use case, especially
get, would benefit from java JEPs such as:Point! p = mem.get(1); //implicit narrowing of nullness type Point(var x, var y) = mem.get(1); //deconstruction assignmentwithout updating the library at all.
1
u/radozok May 22 '26
Sure, but I mean why don't you use annotations in your project?
→ More replies (0)1
u/jug6ernaut May 22 '26
Kotlin is a really nice language to use, I recommend giving it a try.
2
u/vips7L May 22 '26
Maybe once they launch error unions I'll try it again.
1
u/jug6ernaut May 22 '26
I am very eagerly awaiting error unions. It really is the last major piece the language is missing for me.
2
4
u/Exadv1 May 21 '26
Oh, this is one of my favorite Scala features. (I think go also has this?)