3 ms·
Deep value equality can also be expensive to check, so some languages default to reference equality even for immutable values.
by erik_seaberg 3y ago
Deep value equality can also be expensive to check, so some languages default to reference equality even for immutable values.
- mrkeen 3y agoI used to think having both == and .equals in Java was specifically dumb, but the more I think about it, the more I dislike this kind of sneaky reference equality in the general case. Comparing references isn't even an optimised version of comparing values, because when the references don't match but the values do, equality will be false. I struggle to think of when reference equality would be useful to me. Like if I were writing a standard for-loop over an array, it's like I'd be comparing 'i' to 'i'. If == gives me a fast 'yes' response, I feel like I (or my algorithm) should have already known they were going to be the same. If == gives me a fast 'no' response, then do I not actually care about the value?
- erik_seaberg 3y agoI think Python got this right, where "x is y" is identity (not often used but still concise) and "x == y" is value equality (a method you can override).