4 ms·
Technically it's not a problem to define equals based on mutable data, but it is a problem to define hashCode based on mutable data. One could have a class wit
by amalcon 17y ago
Technically it's not a problem to define equals based on mutable data, but it is a problem to define hashCode based on mutable data. One could have a class with, say, an immutable X and a mutable Y. You probably want to check equality based on both X and Y, but hash based on X.
Or, y'know, just don't use mutable data for keys. I thought that was what people were supposed to do anyway.
- stcredzero 17y agoMaybe languages should have special interfaces for things that can be dictionary keys? That way, when someone implements that interface, you are certifying that the object is suitable for being a key.
- pmjordan 17y agoC++'s std::[unordered_]{map|multimap|set|multiset} containers all make const copies of keys. Of course, this being C++ you can wiggle out of this requirement (e.g. using the mutable keyword) but you have to be pretty insistant about it. On the other hand, C++ containers have all sorts of other problems (iterator invalidation to name one)... C# has the value type concept, but I don't think it's enforced on container keys.