Suitability of overriding == (vs ===)?

Some advice, please. Consider a structure like this:

immutable Foo
  x::Int
  y::Int
  s::String
end

(In the past, this type didn’t have s so == just compared x and y.)

I’d still like to have == compare x and y, but ignore s. Because Foo is immutable, === will compare all fields (which is useful as well). I was hoping to override == as follows:

== (a::Foo, b::Foo) = (a.x == b.x && a.y == b.y)

This way, Foo(1,2,"foo") == Foo(1,2,"bar") is true, but Foo(1,2,"foo") === Foo(1,2,"bar") is false.

Are there any unintended consequences to overriding == like this? (I am not doing anything with hashcodes or anything.)

Remember to overload the hash if you want to use any functions that uses hash. Should be fine otherwise. (Other than that Foo(1,2,"foo") == Foo(1,2,"bar") is a bit weird without context.)

2 Likes

Thanks, @yuyichao. Under what circumstances would hash need to be overriden?

if you want to use any functions that uses hash

http://docs.julialang.org/en/latest/search.html?q=hash

1 Like

Maybe @anon94023334 , this package AutoHashEquals will be helpful.