3 ms·
I'd argue the real fix is to use a language that doesn't have insane semantics for ==.
by yxhuvud 13y ago
I'd argue the real fix is to use a language that doesn't have insane semantics for ==.
- Terr_ 13y agoNo, Java's semantics for == are entirely sane and straightforward: It compares primitives by value, and objects by identity in memory. The true problem is that newbies who haven't grasped the difference see that x==y in a sample program and then leap to all sorts of conclusions, overgeneralizing what they observed because they don't understand how it happened.
- yxhuvud 13y agoThe problem is that comparing by value is not a good default for ==. A language should strive to make the common tasks as simple as possible to do right, while not making it impossible to check object equality or some other kind of equality. Compare Ruby: 1 == 1 => true 4711 == 4711 => true 1.object_id == 1.object_id => true 4711.object_id == 4711.object_id => false
- koenigdavidmj 13y agoThe #equal? method does your object_id comparison, by the way. Ruby's default #== for new classes simply performs identity comparison, just like in Java. The difference is that Ruby has operator overloading, so all the builtin classes overload it with something sensible. (This could work in Java, or it could go the .NET route and ban using the == operator entirely until a certain interface (.NET's IEquatable) is fulfilled.)