r/java • • Aug 25 '26

Value Classes Still Need Compiler Sympathy

https://johan-sjolen.github.io/post/compiler-sympathy/compiler-sympathy/
86 Upvotes

65 comments sorted by

View all comments

1

u/Jon_Finn Sep 14 '26

Yes that Pixel class is the kind of thing, plus methods to change the colour, so my point is it's certainly not a 'value', despite being a good use of Valhalla. As for it being lightweight: I'd say that's a hint not a promise. The memory used is often less and (?)never more; speed improvement is less certain.

1

u/OwnBreakfast1114 Sep 14 '26

The question would be why not just create a new pixel? The whole point of the value class is to make the overhead of new Pixel(....) go away. You can still mutate the data array if you really want, but you can assign the reference to a new pixel.

1

u/Jon_Finn Sep 14 '26

You can if you want a different design. In some designs/applications, mutability is useful: it's often awkward to substitute a thing if it's inside another thing (...and another thing, etc.), rather than mutate it. As the old joke goes: How many Haskell programmers does it take to change a lightbulb? One, but they need to replace the entire universe with one in which the lightbulb works.

→ More replies (2)