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

question

#1

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.)


#2

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.)


#3

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


#4

if you want to use any functions that uses hash

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


#5

Maybe @sbromberger , this package AutoHashEquals will be helpful.