As pointed out in @Michael's answer, currently switch
or instanceof
is required for destructuring records patterns. However, that answer does not reflect what is coming down the pipeline.
I talk to the creators of pattern-matching in Java on an infrequent basis (you can ask them questions on the amber-dev mailing list), and the features that have come out in Java 21 are only the start of pattern-matching. We haven't even reached the half-way point of what they want to include in the language for pattern-matching. I don't think we've even reached the 1/4 point.
Now, to answer your specific question, the feature that they are currently deliberating on that is custom built to solve your exact, specific problem is currently called match
(or let
, again, this is work-in-progress, so the name is not even hammered down yet. All of this is still not guaranteed).
Here is how they are thinking it might work: The Future of Java: Records, Sealed Classes and Pattern Matching - Jose Paumard.
Circle circle = ...;
match Circle(var center, var radius) = circle;
// center and radius are binding variables
Now, none of this is guaranteed, as they don't know for a fact that all of this stuff will work the way they expect. If something is not a good fit for the language, they refuse to include it. Hence why none of this is guaranteed. But there is so much more that they are going to try to put into the language.
I would watch the rest of the video. They explore a lot more of what pattern-matching might be in Java. And again, that's not all. If even half of what they described comes out, then we are not even close to the halfway point. The future is exciting for Java!
But yeah, don't use instanceof
or switch
for this. You can, it's not a sin. But they're building the actual feature you are going to want to use for your exact use-case later. So maybe use instanceof
or switch
for now, and then fix them to use the proper thing if/when the actual feature comes out.
Note that the person leading pattern-matching for Java is @BrianGoetz. If he sees this, he will be able to answer this question in better detail than I can.
Also, here is the official Java YouTube channel. They teach about stuff like this all the time. They have a great video series talking about Java 21, as well as Pattern-Matching in Java thus far.
https://www.youtube.com/@java/videos
record
s aren't meant to be destructured per se. At least not with Java 21. I think the destructuring functionality is just something thatswitch
andinstanceof
offer as a way to simplify handling objects – Immedicableinstanceof
orswitch
. Just callpoint.x()
andpoint.y()
. – Hanswitch
statement, and code compiles. – Platerswitch
does.instanceof
doesn't though. – Astyanaxpoint.x()
andpoint.y()
" I suppose this is a matter of code style, but in the JEP they are specifically "forcing" an instanceof, just to use destructuring. – Ailsun