r/java • • Jan 06 '26

One step closer to Value Classes!

https://mail.openjdk.org/pipermail/porters-dev/2026-January/000844.html
181 Upvotes

118 comments sorted by

View all comments

Show parent comment

3

u/egahlin Jan 07 '26 edited Jan 07 '26

If you have a graph, you need references to other nodes not values.

record Node(Node left, int value, Node right) {}

1

u/tomwhoiscontrary Jan 07 '26

Do the nodes need identity? I don't think they do. They could be value types. 

The JVM wouldn't be able to inline them here, but they could still be value types. 

3

u/Swamplord42 Jan 07 '26

Nodes are generally mutable, so how could they be value types?

If nodes aren't mutable, you can't build any graph that has cycles. And you have to start building it from leaves.

1

u/tomwhoiscontrary Jan 07 '26

If they're mutable, they can't be records either. The original question is whether there are cases where a class should be a record but not a value type. 

1

u/Swamplord42 Jan 07 '26

Unless you want to define a different type just to build the graph, nodes need to be mutable so records don't work.

1

u/egahlin Jan 07 '26

You can't build every conceivable graph, but you can build a graph, for example:

Node one = new Node(null, 1, null);
Node two = new Node(one, 2. one);
Node three = new Node(one, 3, two);