I’m having an issue when trying to use hashes of Dict with custom type as keys. Here’s a minimal example that shows my problem:
struct My
x::Vector{Int}
end
Base.hash(m::My)=Base.hash(m.x)
Base.:(==)(m::My,n::My)=m.x==n.x
a=My([1,2,3])
b=My([1,2,3])
println(hash(a)==hash(b))
u=Dict(a=>1)
v=Dict(b=>1)
println(u==v)
println(hash(u)==hash(v))
Running this (using Julia 1.5.3) one gets that a and b have the same hash, that u and v are equal as they should but that u and v do not have the same hash, which is really confusing me. Note that this does not happen if I define a and b as just Vectors of Int rather than using a custom type. Does anyone knows why this happens and how to fix this ?
julia> a = A([1, 2, 3]);
julia> b = B([1, 2, 3]);
julia> hash(a) == hash(b)
false
Above I used the actual type objects A and B in the hash. You could use symbols like :A and :B instead. I’m not sure if there are any reasons to prefer symbols over type objects, or vice versa, when constructing the hash.