2 ms·
Hash functions should generally hash based on a value's identity. Mutable objects passed around by reference have an identity independent of their value; mutabl
by barrkel 17y ago
Hash functions should generally hash based on a value's identity. Mutable objects passed around by reference have an identity independent of their value; mutable objects passed around by value, on the other hand, change their identity when they're modified.
For example, one list isn't equal to another list, even if it has the same elements, if modifying one list doesn't modify the other. If they're not the same, then they shouldn't compare as equal.
This is one reason I think Java's implementation of hashCode() on collection classes isn't very smart. I think .NET gets it right, having GetHashCode() return a consistent value for mutable collections. (Similar comments apply to the corresponding equality operation.)
But mutable objects passed around by value are bad for other reasons, such as the risk of modifying copies when you think you're modifying an underlying value.