How to check whether 2 symbols are indeed pointing to the same object?

julia> struct MM
           x::Int
       end

julia> Base.:(==)(m1::MM,m2::MM) = m1.x == m2.x

julia> m1 =  MM(3)
MM(3)

julia> m2 = MM(3)
MM(3)

m1 == m2 #returns true

I am looking for a way to check whether m1 and m2 identifiers are really pointing to the same object in memory…

I think the === operator is the standard approach.

1 Like

julia> m1 === m2
true

that doesn’t work. Should I override it? if so how?

=== is the only notion of identity that exists in Julia. I believe the exact condition is that two objects are === if no well-formed program could distinguish them, which holds for your m1 and m2. For isbits types, there is no identity beyond the actual data being stored.

Maybe if you explain what you actually want to accomplish we can help come up with a way to do it.

1 Like

I am merely trying to do a variable aliasing analysis on the AST level & runtime checks.
I am assuming there must be a way to get around it using Julia runtime. (Some C handle )???

Unlike in C(++), Julia variable names simply do not map 1-to-1 to memory addresses. As a result, this information is just not available at the language level. This is especially relevant in the case of isbitstype structs, like the one in the original post. You can inspect the generated code to see what will happen at that level (which may depend on optimization flags, etc.), but your exact question just doesn’t have a well-defined answer.

3 Likes

Are you looking for

mutable struct MM
    x::Int
end

maybe? Two instances of mutable data structures can be distinguished from one another.

3 Likes