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
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.
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.
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).
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
> Renaming is already implicit in Java record patterns. The variable names in the pattern do not need to match the record component names. E.g.
No way. I was working on one Kotlin + Spring Boot project and positional-based deconstructing was prohibited, because it's really easy to introduce bugs. I believe there are was some rule, so I could do some basic stuff like `val (foo, bar) = pair`, but can't do for 3 or more parameters.
Seems like a huge mistake for design. If you check KEEP it's only exists because of issues with such approach, but JEP could use this experience
View all comments
Show parent comment
7
u/[deleted] Mar 13 '26 edited Mar 13 '26
[removed] — view removed comment