r/java • u/joemwangi • Mar 17 '26
Updates to Derived Record Creation - amber-spec-experts
https://mail.openjdk.org/archives/list/amber-spec-experts@openjdk.org/thread/QVALXGP2BRN27ISPNJTA3HEKEIMKLWLG/Interesting discussion on the evolution of derived record creation (“withers”). The proposal seems to be shifting from mutation like blocks to a more explicit reconstruction model using obj.new(...), aligning more closely with constructor semantics and pattern matching.
45
Upvotes
View all comments
15
u/davidalayachew Mar 17 '26
I'll start by saying that I was personally very much ok with the "withers" version of this JEP. Just to highlight any latent bias I may have of "it wasn't broke, why fix it?".
My initial reaction to this new approach -- there is some very pretty lipstick on a not-so-pretty pig. Namely (lol), this "reconstruction" is very much carried by the "named parameters" (lipstick), and if you chose to ignore the lipstick for a moment, I think you'll find that this new version encourages the wrong thing in a slightly subtle way.
To jump straight to the point, I think that "withers" being blocks encouraged you to constrain all the logic of creating the new instance inside the braces, limiting scope (good). I think this new proposal, by sacrificing blocks, encourages me to scope all of my helper variables and WIP objects to the same level, making my code less clear and more spread out, rather than being contained and constrained to just the block.
Let me give an (obfuscated) example. This is code I was working on yesterday.
With "withers", I can instead write this.
Here, I am encouraged to scope the
DataModelto purely the "wither", as that is the only place it is being used. And, since I am already forced to create a block, it is no extra effort to just use that block.Compare that to "reconstruction".
On the one hand, this looks quite similar to the code I was already going to write. So, it is more immediately familiar.
But on the other hand, that's also kind of a bad thing in this specific scenario. That
DataModelis only to be used to constructnewAbc. And yet, I am subtly encouraged by the language to just introduce thedataModelinto the same scope I am already working with. Encouragement, meaning that, since it would take me extra effort to create an arbitrary block to scope the variable, that friction deincentivizes me from doing things "the right way". That's what I mean by "encouragement" -- what the language does and does not incentivize me to do, via friction.In the same vein that "mutable by default" encourages some not-so-great behaviour by developers, I feel like this "reconstruction" is doing the same on a smaller scale.
But back to my point -- familiarity can be a good thing, but that's assuming that what you are being encouraged to do is a good thing. It's familiar because Java itself kind of encourages scoping everything on the same level. But since that is bad, the familiarity is kind of bad here too, by extension.
Imo, this isn't a deal breaker. But I still feel like this new "reconstruction" style's semantics encourages something slightly worse, and thus, is an inferior proposal to the "withers" proposal.
But if I get either one, I'll be happy. Both proposals are solid, and I don't picking either path would be a mistake.