Hi there,
How do I make my custom type instance equal in Julia.
Why two instances with identical values on each field return me 'false' in
this case?
Thank you.
type Pair
a::Int
b::Int
end
x = Pair(1,2)
y = Pair(1,2)
println (x==y)# returns false
If you make the Pair immutable then it should work out of the box.
(Note that Pair is a datatype in 0.4, so probably you should either use
something compatible or something else).
Immutables are compared by the value of their fields, whereas mutables
by ===, i.e. whether they are same object in me
For a generic solution that works on all new types you can create a common
ancestor and define
function ==(a::Ancestor, b::Ancestor)
if isa(a,typeof(b)
na=names(a)
for n in 1:length(na)
if getfield(a,na[n])!=getfield(b,na[n])
return false
end
en
But it might be a bit slow. Did you try it?
Wrote it from the phone so not at the time, have used a similar solution
where at first the initial if was missing so different types with the same
arguments returned true. Just did a benchmark with 3 cases, one with the
literal function above (after fixing the missing ')'), one with n directly