Default `==` for struct and mutability


I was trying to understand how the default == function for user defined struct is working. I know it recursively calls === on all fields. Thus, my assumption was that for struct containing only immutable fields, it behaves as if using == on all fields (as == and === will be the same for immutables (is that even right ?)).
However, then I stumbled on weird stuff. First I tried it with strings (which the documentation say are immutables). However, trying ismutabletype(String) returns true… Then i looked up the documentation for === which states " Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are compared. If those are identical, mutable objects are compared by address in memory and immutable objects (such as numbers) are compared by contents at the bit level. This function is sometimes called “egal”. It always returns a Bool value.".
From this I thought : “Well if strings are mutables and mutables are compared by adress, let’s define 2 different variables containing the same value (which will point to different adresses) and try ===”. However :

a = "foo"
b = "foo"
a === b

returns true. Thus I guess strings must be immutables (as stated in doc) but then why ismutabletype(String) returns true ? Note : all of this is the same for Symbol type (ismutabletype(Symbol) is true but defining 2 variables with same values are ===).

And to go back to my first question, is that true that default == for user defined struct will behave as if == is applied on each fields if and only if all fields are immutables ?


Strings are technically mutable but we pretend really hard that they aren’t, and doing so makes everyone’s life better.

1 Like
julia> -0.0 == 0.0

julia> -0.0 === 0.0

julia> NaN == NaN

julia> NaN === NaN

julia> isequal(NaN, NaN)

julia> x = reinterpret(Float64, -1);

julia> NaN === x

julia> isequal(NaN, x)

julia> NaN == x