# Default \`==\` for struct and mutability

**URL:** <https://discourse.julialang.org/t/default-for-struct-and-mutability/91711>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 15, 2022, 8:14pm UTC](https://discourse.julialang.org/t/default-for-struct-and-mutability/91711 "2022-12-15T20:14:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyril](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@Cyril](https://discourse.julialang.org/u/Cyril)\
**Post date:** [December 15, 2022, 8:14pm UTC](https://discourse.julialang.org/t/default-for-struct-and-mutability/91711/1 "2022-12-15T20:14:49Z")

</div>

Hello,

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 :

```julia
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 ?

Best,  
Cyril

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 15, 2022, 8:26pm UTC](https://discourse.julialang.org/t/default-for-struct-and-mutability/91711/2 "2022-12-15T20:26:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 15, 2022, 9:17pm UTC](https://discourse.julialang.org/t/default-for-struct-and-mutability/91711/3 "2022-12-15T21:17:12Z")

</div>

> [@Cyril](#):
>
> as `==` and `===` will be the same for immutables (is that even right ?)).

```julia
julia> -0.0 == 0.0
true

julia> -0.0 === 0.0
false

julia> NaN == NaN
false

julia> NaN === NaN
true

julia> isequal(NaN, NaN)
true

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

julia> NaN === x
false

julia> isequal(NaN, x)
true

julia> NaN == x
false

```
