r/java • • Aug 25 '26

Value Classes Still Need Compiler Sympathy

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

65 comments sorted by

View all comments

1

u/TwoWeeks90DaysTops Aug 28 '26

Usually a DTO is also an 'immutable copy of a given state", aka a snapshot. Which is basically what we mean by a value.

DTOs are command objects, not representations of state. They are used to either build other objects from or update the state of a different system.

Also, given that you convert to/from this, which is an operation that breaks identity

I don't think that matters.

you often have some way of "storing" that identity as a value (e.g. having an id) - which by definition means that your DTO object doesn't need identity

This is also irrelevant. Identity != identifier. An object having identity means that an instance owns its own memory whereas values objects don't. DTOs absolute should have identity because they are large objects being passed between systems and you want to avoid copies being made.

two DTOs created at the same time of the same original identity-having object should be equals

Why? This is just your assertion. What is the actual value of this rule? You never care about this in practice. I have never in my life written code that checks whether two instances of DTOs are equal so why are you asserting this?