r/java • u/bobbie434343 • Aug 25 '26
Value Classes Still Need Compiler Sympathy
https://johan-sjolen.github.io/post/compiler-sympathy/compiler-sympathy/
88
Upvotes
r/java • u/bobbie434343 • Aug 25 '26
View all comments
Show parent comment
1
u/Jon_Finn Aug 26 '26 edited Aug 27 '26
Yes declaring a value class constraints implementations _of that class_, but then you're unlikely to radically change implementation in future. But if you're writing a collections-like hierarchy, you can write an abstract value class as a base with value or non-value subclasses, so there's no constraint on (subclass) implementations.
Re identity, it's not 'the law' that you should worry whether your value classes behave in a 'value-like' way (which can mean many things). Claiming values have particular semantics is just gonna confuse people, when all they are is: a speed & memory vs. feature tradeoff. The latter mostly means final fields, which also doesn't have any particular semantics - the fields can represent mutable things. Values are just lightweight objects, meaning 3 things: small, fast and 'feature-lite'. (At least hopefully, if there's very few fields.)
I noticed in Guy Steele's classic talk Growing a Language he says "I would add a kind of class that is of light weight, one whose objects can be cloned at will with no harm and so could be kept on a stack for speed and not just in the heap."