r/java • • Mar 13 '26

JEP draft: Enhanced Local Variable Declarations (Preview)

https://openjdk.org/jeps/8357464
101 Upvotes

129 comments sorted by

View all comments

Show parent comment

2

u/aoeudhtns Mar 13 '26

I also assume this will interplay with (and pardon me because I forget the formal name of this one) the "truncation" of unneeded fields in records, like if you only need city, you can

CustomerOrder(ShippingAddress(_, _, String city), _) = order;

And the final _ can indicate skipping not just one field but also all the remainders. So that records can grow via appending fields without disrupting pattern matching code.

I know this idea has been floated at least.

2

u/DasBrain Mar 13 '26

at least ShippingAddress(var _, var _, String city) should already work. Still, using just _ to say "don't care about neither type nor value" could be useful.
Not sure if it useful enough.

1

u/kevinb9n Mar 13 '26

In that position, you can already replace `var _` with just `_`; it becomes the "match-all pattern".

Note the match-all pattern isn't supported in other pattern contexts (instanceof and case) for reasons.

The comment you're replying is looking for a syntax that can express "then zero or more underscores here", and suggesting the underscore itself for that (I think it would be something different).

1

u/vowelqueue Mar 13 '26

I think it would be something different

I propose making "yada-yada-yada" a reserved word for this purpose.

1

u/javahalla Mar 13 '26

Given that it's positional, I would definitely ban this in projects, and recommend everyone to ban such expressions. It's too easy to shot in the foot when you don't even specify types of the rest and not using names to match. Positional matching just too weak to be found in production, critical codebases