Records are shallowly immutable, whether they have identity or value semantics. With Valhalla adding value records, what situations still call for an identity record?
When would an identity record actually be useful?
Answers
The distinction is less about mutability and more about whether two separate objects with the same components should count as the same thing. Think of graph vertices or other nodes: two nodes can have matching fields and still be distinct vertices. Identity-based collections and algorithms can preserve that distinction.
So an identity record can be a compact way to describe an immutable node while keeping object identity. A value record is a better fit when the components themselves define the whole meaning of the object.
Reply to this answer
There are two questions mixed together here: what identity records are for, and which kind of record should be the default.
Existing records have identity, so silently making them value-based would change observable behavior, including reference comparisons and identity-based collections. Keeping identity as the default preserves that behavior. Choosing value semantics can then be an explicit decision when identity does not matter.
Reply to this answer
Identity records are still immutable records. They just retain distinct object identity, which matters if you need to distinguish equal-looking instances or use identity-based tracking. Synchronizing on one is possible too, though that seems like a weak reason to choose the type.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions