Record patterns in java without instanceof nor switch
Asked Answered
P

1

6

Java 21 record patterns promises the introduction of destructuring to the Java language. However, it seems to be tightly coupled to pattern matching that can only be used as part of instanceof comparison or in switch statements/expressions.

Consider the following record.

public record Point(int x, int y) {}

Is there a way to destructure an object of that particular type without using instanceof or switch? The following attempts do destructure point. But the code makes little sense as neither instanceof nor switch are unnecessary if we assume null-safety.

Point point = new Point(0, 0);

// destructuring with instanceof
if (point instanceof Point(int x, int y)) {
    System.out.printf("Point at (%d,%d)", x, y);
}

// destructuring with switch
switch (point) {
    case Point(int x, int y) -> System.out.printf("Point at (%d,%d)", x, y);
}
Plater answered 22/9, 2023 at 12:53 Comment(9)
I'm not entirely sure but I think records aren't meant to be destructured per se. At least not with Java 21. I think the destructuring functionality is just something that switch and instanceof offer as a way to simplify handling objectsImmedicable
I'm not an expert on new features in Java 21 but I think the basic answer is no, and you shouldn't try to things like forcing an instanceof or switch. Just call point.x() and point.y().Han
"if we assume null-safety" That's a big if. Java doesn't "assume null-safety".Astyanax
"Java doesn't 'assume null-safety'". In a way it does; I don't have to provide a default clause in the switch statement, and code compiles.Plater
Okay, apparently switch does. instanceof doesn't though.Astyanax
@Han "you shouldn't try to things like forcing an instanceof or switch. Just call point.x() and point.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
@Immedicable and OP, I posted an answer that addresses your point. Long story short, the creators of pattern-matching in Java are actually considering a feature that is custom built to solve your exact problem. Check it out.Sublimation
Adding @markspace, I've addressed this point in a separate answer.Sublimation
Also adding @AstyanaxSublimation
S
10

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

Sublimation answered 22/9, 2023 at 16:15 Comment(7)
Good video link, but I agree, this entire answer can be much improved. I personally find the bold, italic and caps style very annoying. You need 3 different kinds of emphasis? I would sum this up in one sentence "It doesn't exist yet but they're thinking of adding it in the future using a new keyword. [video link] [code example]"Ailsun
@Holger I've de-loled the answer. Although it conveyed that the answerer found those particular facts humorous, it didn't really convey anything of substance to the answer itself.Fagen
Hello all. Fair criticisms. Thank you @M.Justin for the clean up.Sublimation
@Ailsun I am incredibly excited about this coming future for Java. It is going to enable me to do things that would have been so much harder and error prone previously. That is where the ALL CAPS, BOLD AND ITALICIZED comes from.Sublimation
@Holger As for the lols, see above comment.\Sublimation
I understood that you are very excited and it’s comprehensible. But here, “lol” has a negative connotation for those who didn’t know about it yet. And in general, we try to keep emotions out of the posts and try to present the facts.Lamellar
@Lamellar Fair enough, understood.Sublimation

© 2022 - 2025 — McMap. All rights reserved.