4 ms·
For value equality, I feel like the whole value/reference type distinction is an unnecessary distraction. What we really need is different operators to handle o
by int_19h 8y ago
For value equality, I feel like the whole value/reference type distinction is an unnecessary distraction. What we really need is different operators to handle object identity comparisons and value comparisons. Whether an object can be meaningfully compared by value to another object is orthogonal to whether it's a value type or not - e.g. arrays are reference types, but there's no reason why any two arrays can't be compared element-by-element.
One language that gets it mostly right is Python, where "==" always compares values, and "is" always compares references. Thus, you can use either for e.g. lists and sets, depending on what you actually need at any given time. However, Python fails in that it doesn't give the same flexibility in many contexts where the comparisons are implicit - e.g. in dicts and sets (it always uses ==, instead of letting the user specify an equality function).
Ironically, a language that gets it fully right - including the standard library - is VB.NET.
I'm not even sure that value types themselves are such a useful concept. A value type can be thought of as an immutable reference type, where any mutations done via the reference simply cause a new object to be created and the reference changed (semantically; under the hood, it can be easily optimized down to in-place update).