[julia-users] custom type equality

2015-04-18 Thread Kuba Roth
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

Re: [julia-users] custom type equality

2015-04-18 Thread Mauro
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

Re: [julia-users] custom type equality

2015-04-18 Thread Marcus Appelros
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

Re: [julia-users] custom type equality

2015-04-18 Thread Toivo Henningsson
But it might be a bit slow. Did you try it?

Re: [julia-users] custom type equality

2015-04-19 Thread Marcus Appelros
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