7 ms·
JEP Draft – Derived Record Creation (Preview)
- davidalayachew 3y agoJava "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!
- DarkNova6 3y agoJust recently, talks in the java amber mailing list emerged talking precisely about deconstruction of arbitrary classes.
- davidalayachew 3y agoYes, I think I was participating in those talks. That does raise a good point -- If this concept might one day go over to the every day class, it sounds like they might be forced to list the deconstruction pattern used to do "reconstruction". Either way, my main point is that -- the implicitness of this feature is appreciated, but I don't know if I want to be forced between implicit or just not using this feature, since my uninformed gut feels like I would appreciate the explicit clarity of seeing the variables I am working with. It might feel like a step back, but again, this is my opinion before having a chance to try this thing.
- eweise 3y agoWhy wasn't this included in the initial records implementation? I tried records and quickly gave up after realizing there was no copy method. There's plenty of prior art. Scala has had this for twenty years. Java needs to stop shipping features that don't work as well as in other languages.
- pjmlp 3y agoOne step at a time, that is why. Scala is also on the edge of turning into Coffee Script, its relevance wanning. 20 years is pushing it, by the way.
- eweise 3y agoOh yes, 18 years. Guess that's not enough time. And yes, Scala's relevance is waining but not sure what that has to do with this topic. Kotlin also has a copy method if you need an example of a language doing well.
- pjmlp 3y agoKotlin is only doing well on Android, because mama Google tells so to all the kids targeting Android that they have to eat Kotlin no matter what. Java is doing quite well on the Java Virtual Machine, it isn't going anywhere for not adopting every feature from Beanshell, Groovy, jTcl, jython, Frege, Ruby, Scala, Kotlin, and whatever else comes that needs to implement on top of Java to exist in first place.
- eweise 3y agoNo need to get defensive about Java. I developed with it for over 20 years. But personally, I left it because I find it too painful to code in. Every once in a while I try the latest version and it continues to disappoint, records are a good example. I've also been working in fintech for the past 7 years and the companies I worked for, don't want to use Java. Its either been Kotlin/Spring because the syntax is much nicer, or Go because it doesn't have all the JVM baggage. Maybe Java is still really popular but fortunately, its also easy to avoid now.
- ecshafer 3y agoThis 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.
- tadfisher 3y agoThis concerns me: Point newLoc = oldLoc with { x *= 2; y *= 2; z *= 2; }; It feels more like a mutable "Builder" class than a copy constructor. I guess the benefit is that you can do things like the above, instead of the following in Kotlin: val newLoc = oldLoc.copy( x = oldLoc.x * 2, y = oldLoc.y * 2, z = oldLoc.z * 2 ) But in Kotlin it's more clear that oldLoc is immutable.
- rbehrends 3y agoFor what it's worth, in Kotlin you could also do the following: val newLoc = oldLoc.run { copy(x = x * 2, y = y * 2, z = z * 2) }
- zaphirplane 3y agoThe syntax is definitely strange and not Java like, it doesn’t match any Java syntax I recall. Not a lambda, not a method call, not a keyword or even a static method It should be 1 of the above. They also made the same weird new syntax choice with string template. One of the most important things about a language is coherence and consistency. That’s why a language designer is a role/talent
- inglor 3y agoC# does this already https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/with-expression 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.
- nonsensikal 3y agoI thought of F# record expressions immediately https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/copy-and-update-record-expressions https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...
- zokier 3y agoThe C# with seems bit more restricted though because it uses object initializer syntax while Java has full-blown code block, so in Java you can put arbitrary statements in. Also in Java you get the original values in the with statement, which I don't see in C#.
- marwis 3y agoAlso C# records do not support initialization code so you can't validate arguments :(
- lpapez 3y ago[flagged]
- davidalayachew 3y agoIt is a bit clunky, but it makes things really traceable. The Java team even backported their old, historical bugs/requests-for-enhancement into JIRA. You can see feature requests from the late 90's in there. Plus, you can write little SQL-esque queries to filter down submissions to just what you want. So, true to Java form -- it's clunky, but the value is real.
- marwis 3y agoSadly non-contributors cannot submit issues and comments nor observe tickets :(
- davidalayachew 3y agoI know we don't have the ability to follow/get notifications about a ticket, but we do have the ability to submit issues and comments by proxy! If you lack an account, go to the Java Bug Database -- https://bugs.java.com/bugdatabase/ https://bugs.java.com/bugdatabase/, and about halfway down, you will find options for submitting a bug or a Request-for-Enhancement (RFE). You check some boxes, fill out a form, and press submit. Then, a member from the Java team or Java's bug reporting team (usually ANUPAM) will get your ticket submitted to the JIRA. They will also email you, notifying you that your ticket was approved to be added. When they notify you, they also provide you a point of contact email. You can can send comments on your ticket by emailing this point of contact email. And finally, if you want to comment on tickets that do not belong to you, you can reach out to ANUPAM and request them to forward your comment. But they might be hesitant if it is not your ticket.
- marwis 3y agoI know this and I hope you realize how horrible this is for community engagement. I'm surprised there isn't an option to send your request via post mail. Also other community-hostile behaviors of Java/OpenJDK project: - mailing lists have no search function and block search engines/crawlers by returning 403 (apparently robots.txt is not enough!) - can't post to mailing list without subscribing despite information that such messages will just need to be approved by moderator (they never are)
- etwigg 3y ago[flagged]
- deleted 3y ago[deleted]
- pulse7 3y agoThe 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!
- davidalayachew 3y agoThe general idea behind JEP Drafts such as these is that they first focus on the semantics -- what can/should this feature do? They save things like syntax until the very end, right before they decide to submit this as an actual JEP instead of a JEP Draft. This JEP entered the Draft stage only a few hours ago. All that is to say -- your comment makes sense, but you are criticizing the part of this JEP Draft that is not only the least relevant to the discussion, but the thing that is most likely to change before it exits the Draft stage. Oftentimes, they even put in placeholder syntax to highlight this fact. I'd limit criticisms of syntax until they submit this JEP for actual review.
- marwis 3y agoWell, records are immutable so assignment must go to newly constructed instance, meanwhile new instance does not exist yet so any read must be from old instance. Seems very intuitive to me (and same as C#).
- pulse7 3y agoIf it is intuitive one does not need an explanation...
- funcDropShadow 3y agoTherefore, everything you already know is intuitive. Oh wait, does that mean that intuitive is not a property of this proposal but of the reader and the proposal? Ergo, it is almost useless to ask whether some programming language feature is intuitive or not.
- davidalayachew 3y agoCould 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.
- davidalayachew 3y agoAlso, I don't know how to use HN. How does one flag down a mod?
- davidalayachew 3y agoI learned how btw -- you email them at hn@ycombinator.com. A shame that you can't ping them directly through this application (like you can in Reddit), but the response time was quick, so that is appreciated.
- dang 3y agoFixed now!
- davidalayachew 3y agoTy vm!
- mangoo84 3y ago[dead]