from Hacker News

JEP Draft – Derived Record Creation (Preview)

by davidalayachew on 1/24/24, 6:32 PM with 50 comments

  • by davidalayachew on 1/24/24, 6:41 PM

    Java "withers" are on the way!

    I am very excited for this feature and has been a long time coming. This will make immutable transformation code much easier to write and reason about.

    That said, I'm a little uneasy with the idea that we don't even have the option to write out our deconstruction pattern. That feels like a foot gun. I know that records come pre-baked with a deconstructor, I'm not asking to be able to choose. I am more asking for the ability to see the variable that I am overwriting. And I am not asking that every Java developer should have to shoulder that burden -- keep it implicit for those who want it. But I feel uneasy that I don't even have the option to write out the variables in my deconstructor.

    I will try it out before taking issue with it, but these are my initial, uninformed feelings from looking at it. Regardless, even if my fears become true, this feature is well worth it, and I'm very happy to have it!

  • by inglor on 1/24/24, 8:35 PM

    C# does this already https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... - works pretty well. It's good that Java is finally getting more ergonomic these past few years.
  • by davidalayachew on 1/24/24, 10:10 PM

    Could a moderator change the title back to at least include the phrase JEP Draft?

    That is important because it communicates that this feature is actually not yet in Preview, but is on its way to being that.

    Being in the JEP Draft state sets expectations about what part of this proposal is up for debate and criticism, and what parts are merely placeholder until the "heavier topics" have been hammered out.

    Because you changed the title to this, it is actually not communicating the true intent, and thus, might invite discussion (read - criticisms) that this JEP Draft was never meant to contest.

  • by ecshafer on 1/24/24, 8:11 PM

    This looks like it could be a good change. Personally I think new point with { x = 1, y = 2, z = 3 } is much cleaner looking than say point.withX(1).withY(2).withZ(3). names params is generally something I like it languages though, so I might be biased here.
  • by pulse7 on 1/24/24, 9:17 PM

    The proposed syntax is not intuitive... How can I know to which records x, y and z are refering to in: Point newLoc = oldLoc with { x = 2; y = 2; z *= 2; } ? And in this case "return this with { im = -im; };" - which "im" goes to which record? Please make more intuitive syntax for this!