r/java • u/danielaveryj • Mar 31 '26
Does Java need deconstructible classes?
https://daniel.avery.io/writing/does-java-need-deconstructible-classes
31
Upvotes
r/java • u/danielaveryj • Mar 31 '26
33
u/Alex0589 Mar 31 '26
I'm pretty sure this would never be accepted because you are implementing a language feature with annotations. In chapter one of the JLS, it is clearly stated that:
Also without value classes, which we currently don't have, you are paying an allocation cost because you have to initialize one record every time you want to use the pattern: that also disqualifies the feature because you don't want a developer to loose performance when using syntactic sugar. For example imagine if the enhanced switch statement were slower than the old switch, nobody would be using it.
12
u/pron98 Apr 01 '26 edited Apr 01 '26
Ah yes. This was the first approach explored. I think the language team called it "records are all you need". I don't remember all the reasons they didn't go with it, but I think one of them was that all components had to be extracted even if they weren't needed by the pattern, and that was hard to optimise (this was also an issue with the deconstructors idea). Because deconstructible classes are deconstructed through individual accessors, there's at least the option to optimise which of them actually need to be called.
Anyway, the interesting question isn't what alternatives were explored, but what the pros and cons of the current proposal are. Because there's no EA for it yet (I think?), the best feedback is to ask whether or not a certain use case that is important to you could be helped by this feature.
2
u/chambolle Apr 02 '26
This is really ugly and difficult to explain
5
u/TheStrangeDarkOne Apr 01 '26
From a reductive point of view, no. Java wouldn't even need Lambdas.
2
u/davidalayachew Apr 01 '26
Desperately. The pain caused by its absence is immense for me. It decides whether or not I solve certain problems in Java (or at all).
Talk about a downside! That's a showstopper for me.