Concretely:
julia> type Test
x::Int
end
julia> Base.hash(t::Test) = hash(t.x, hash(:Test))
julia> Base.:(==)(a::Test, b::Test) = a.x == b.x
julia> a = Set{Test}()
Set{Test}()
julia> push!(a, Test(1))
Set([Test(1)])
julia> push!(a, Test(1))
Set([Test(1)])
On Friday, May 27, 2016 at 2:19:04 PM UTC+2, Mauro wrote:
>
> I think you need to define hash for your type too as Set is based on
> Dict. Read up here:
>
> http://docs.julialang.org/en/release-0.4/stdlib/collections/?highlight=hash#associative-collections
>
>
> On Fri, 2016-05-27 at 09:40, Dario Prandi <[email protected] <javascript:>>
> wrote:
> > Dear all,
> >
> > while experimenting with the Set collection I am incurring in a very
> strange
> > behavior when I use a custom type. More precisely:
> >
> > julia> type Test
> > x::Int
> > end
> >
> > julia> import Base.==
> >
> > julia> ==(a::Test, b::Test) = a.x == b.x
> > == (generic function with 110 methods)
> >
> > julia> a = Set{Test}()
> > Set{Test}()
> >
> > julia> push!(a, Test(1))
> > Set([Test(1)])
> >
> > julia> push!(a, Test(1))
> > Set([Test(1),Test(1)])
> >
> > This should not happen, since Test(1)==Test(1). Moreover, I get the
> following
> >
> > julia> Test(1) ∈ a
> > true
> >
> > julia> haskey(a.dict, Test(1))
> > false
> >
> > which is quite strange, since the definition of the∈ function is
> >
> > in(x, s::Set) = haskey(s.dict, x)
> >
> > Finally, I remark that the following works correctly
> >
> > julia> x = Test(1)
> > Test(1)
> >
> > julia> a = Set{Test}()
> > Set{Test}()
> >
> > julia> push!(a,x)
> > Set([Test(1)])
> >
> > julia> push!(a,x)
> > Set([Test(1)])
> >
> > Someone has any idea of what is happening here? I'm on v0.4.5, butthe
> same
> > behavior is reproducible onv0.5.
> >
> > Thanks,
> > Dario
>