Wednesday, 6 April 2016

Julia: Check for objects equality

By using ‘is’ function, you can check whether two objects are equal or not.

How ‘is’ function compares two mutable objects?
Compares mutable objects by address in memory, if both the objects point to same memory location then is function return true, else false.

julia> type Employee
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int64
       end

julia> 

julia> emp1=Employee("Hari Krishna", "Gurram", 1)
Employee("Hari Krishna","Gurram",1)

julia> emp2=Employee("Hari Krishna", "Gurram", 1)
Employee("Hari Krishna","Gurram",1)

julia> emp3=emp1
Employee("Hari Krishna","Gurram",1)

julia> is(emp1, emp2)
false

julia> is(emp1, emp3)
true

julia> is(emp2, emp3)
false

As you observe above snippet, even though all the three objects emp1, emp2, and emp3 has same contents, emp1 and emp2 are not equal, since they point to different memory locations.

How ‘is’ function compares two immutable objects?
Compares immutable objects by contents at the bit level.



Previous                                                 Next                                                 Home

No comments:

Post a Comment