3 ms·
It is an identity you can't get rid of, and which is often misused, or accidentally used, to create a lot of complexity. No, you can't override '==' in Java and
by jstimpfle 10y ago
It is an identity you can't get rid of, and which is often misused, or accidentally used, to create a lot of complexity. No, you can't override '==' in Java and other OO languages. You can override .equals() which defaults to ==.
> A partial fix? It is how you solve the problem with objects. I am not sure what other method you would propose.
You mentioned it yourself: structs or records, a.k.a value semantics. Here is a resource from people with more authority (esp. Guy Steele) http://cr.openjdk.java.net/~jrose/values/values-0.html http://cr.openjdk.java.net/~jrose/values/values-0.html
- lacampbell 10y ago> It is an identity you can't get rid of, and which is often misused, or accidentally used, to create a lot of complexity. No, you can't override '==' in Java and other OO languages. You can override .equals() which defaults to ==. Using Java as an example to prove something about OO isn't very useful IMO. It'd be like holding up rust as an example for how functional languages do things - yes, it's fairly functional but hardly canonical. In ruby, for example, you can indeed override ==. > You mentioned it yourself: structs or records, a.k.a value semantics. Here is a resource from people with more authority (esp. Guy Steele) http://cr.openjdk.java.net/~jrose/values/values-0.html http://cr.openjdk.java.net/~jrose/values/values-0.html I think you're conflating structural equality on the one hand, and reference semantics on the other. The two aren't related in real OO. For example, here's a class that defines equality the same way an ML record might: class Value def ==(other) if other.class != self.class then return false end self.instance_variables.each do |v| if instance_variable_get(v) != other.instance_variable_get(v) then return false end end return true end end Then we can use it to define a simple point class: class Point < Value def initialize(x, y) @x, @y = x, y end end And there you go - structural equality, but still passed as references. Point.new(0, 0) == Point.new(1, 2) => false Point.new(0, 0) == Point.new(0, 0) => true