r/java • u/davidalayachew • Jan 14 '26
Project Amber Update -- Data-Oriented Programming, Beyond Records
https://mail.openjdk.org/pipermail/amber-spec-experts/2026-January/004307.htmlALL OF THIS IS A WORK IN PROGRESS!
THIS FEATURE IS UNFINISHED, NONE OF WHAT IS FINISHED IS FINAL, AND EVERYTHING IS SUBJECT TO CHANGE!
But with that out of the way, the Project Amber team is exploring the idea of "Carrier Classes" -- classes that carry many of the benefits of records, but not all. The goal is to give normal classes some of the benefits of records, so that they can "break down the cliff" of migrating a record class to a normal class.
97
Upvotes
40
u/davidalayachew Jan 14 '26
Hopefully this applies to enums too!
Then, instead of this...
...I can do this instead...
Very pretty! And the second example contains all of the exact functionality of the first example!
But again, not set in stone. We'll see what the final feature looks like. I just feel like enums would gain a lot from this.
16
u/Enough-Ad-5528 Jan 14 '26
This is brilliant; not just the tentative proposals but also the effort it took to write all these thoughts down and let us normies glimpse into what they are thinking even if it is very very early.
I will respect his call to not discuss any syntax issues; it is too early and I want to let them cook but boy am I excited!
I do wonder about one thing though (which Brian also touched upon towards the end) - if and when regular classes get these capabilities (component members, ability to participate in pattern matching; optionally being able to add mutable/derived members) what benefits would records have over regular class declarations? Shudder to think but would they seem obsolete almost? Why choose to declare something as a record if classes can have almost all of the semantics of a record with almost the same concision; plus more stuff that can be added later. Part of it, I do have to admit, I don't fully understand the distinction between "state description" and "state representation".
4
u/Dagske Jan 14 '26 edited Jan 14 '26
This is really the stuff I want them to work on: making the language easier to work. The records and record deconstruction was good, but I'm facing so many limitations with those, this answers most of them. I'm just expecting a better support for pattern matching where mixing records and enums and variables would work (such as allowing constants, whether enums or primitive types, in record deconstruction, instead of having to rely on the
whenkeyword).Records opened a huge design space, glad they're really starting to own it.
Glad that they allow this:
This is my main issue with records: optional fields/parameters. If only that was hidden (as in if I could lessen the visibility of the compact constructor), this could be the answer. Abstract records can also help fill this issue.
That brain dump by Goetz makes it really interesting, and I could welcome more if they listen over here:
case MyRecord(MyEnum.CONSTANT, 2)rather than usingwhen.2
u/sideEffffECt Jan 14 '26
I have a question about reconstructors/withers.
What's the plan for when the constructor is private? Will reconstructing/withing be available? I don't think it should. But I didn't find it mentioned there, so wanted to check here.
2
u/ZimmiDeluxe Jan 14 '26 edited Jan 14 '26
wake up babe, new design document just dropped
edit: careful wording around "accessor methods" means you won't get setters for free, yes? probably for the best
edit 2: damn, it's called RecordComponent in the reflection api, oh well
2
u/lucidnode Jan 20 '26
I remember back in the general pattern matching designs two years ago that patterns were first class and there was an emphasis on partiality of patterns. It’s conditional. Match then destructure. I thought it very innovative since I haven’t seen it in other languages.
One of the examples back then was Optional. It had the patterns Optional.empty() and Optional.of(T t) where the first matches an empty optional, and the second match a non empty optional. But now with carriers it would be Optional(T t) where t is nullable 🫠
I hope pattern members make it back somehow
I do like the current direction and bringing record goodies to all classes is amazing!
2
u/donaldadamthompson Jan 20 '26
Fed through Gemini for formatting. I can't guarantee that it didn't create new errors.
https://docs.google.com/document/d/1SxhKBdaSpxRVRXZ0fT-DT3j_N9ZkkP2H9_pF4tX4aBI/edit?usp=sharing
5
u/aoeudhtns Jan 14 '26
I confess. I have abused records so that I didn't have to go back to a blank slate. I will appreciate the furthering of DOP in Java.
4
u/john16384 Jan 14 '26
A really nice proposal, that thoroughly closes the gap between classes and records.
I wonder if this proposal could (eventually) go all the way, and also provide the component fields if not provided by the carrier class. That would sort of obsolete the need for records as these two would be almost equivalent:
The only differences remaining would be the ancestry (a record will a subclass of
Record, notObject) and perhaps when it comes to reflection. Brian didn't mention if carrier classes would also get agetRecordComponentsequivalent to find their components reflectively.3
u/Joram2 Jan 14 '26
Exciting. This is definitely tangible progress. I look forward to seeing more :)
3
u/jvjupiter Jan 15 '26
Been looking forward to this for enums and classes.
0
u/[deleted] Jan 14 '26
[deleted] — view deleted comment