2 ms·
The 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
by yxhuvud 13y ago
The 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.)