# Why two composite types with all bitstypes members are different if they are defined mutable?

**URL:** <https://discourse.julialang.org/t/why-two-composite-types-with-all-bitstypes-members-are-different-if-they-are-defined-mutable/118267>\
**Category:** General Usage\
**Created:** [August 16, 2024, 10:23am UTC](https://discourse.julialang.org/t/why-two-composite-types-with-all-bitstypes-members-are-different-if-they-are-defined-mutable/118267 "2024-08-16T10:23:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 16, 2024, 10:23am UTC](https://discourse.julialang.org/t/why-two-composite-types-with-all-bitstypes-members-are-different-if-they-are-defined-mutable/118267/1 "2024-08-16T10:23:04Z")

</div>

As from the title: I am a bit confused on why two composite types with all bitstypes members are different if they are defined mutable ?

```julia
mutable struct Foo
    x::Bool
end
struct Goo
    x::Bool
end

julia> f1 = Foo(true)
Foo(true)
julia> f2 = Foo(true)
Foo(true)
julia> f1 == f2
false
julia> g1 = Goo(true)
Goo(true)
julia> g2 = Goo(true)
Goo(true)
julia> g1 == g2
true

```

Let me see if I got it while writing this question… the `Goo` instances do memorize the actual bool value, while the `Foo` instances memorize only the memory address of them. This memory address is different between `f1` and `f2`.  
Still, I would have expected the `===` operator to fail, while I would have expected `==` to lookup these memory addresses and look that the actual bit values are the same (true) for both objects… 😵‍💫

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 16, 2024, 11:33am UTC](https://discourse.julialang.org/t/why-two-composite-types-with-all-bitstypes-members-are-different-if-they-are-defined-mutable/118267/2 "2024-08-16T11:33:41Z")

</div>

`==` falls back to `===` by default, it doesn’t recurse into fields automatically. Look into AutoHashEquals.jl if you need/want that.

---

<div class="post-metadata">

**Author:** ![mohamed.d180](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed.d180/32/52028_2.png) [@mohamed.d180](https://discourse.julialang.org/u/mohamed.d180)\
**Post date:** [August 16, 2024, 12:07pm UTC](https://discourse.julialang.org/t/why-two-composite-types-with-all-bitstypes-members-are-different-if-they-are-defined-mutable/118267/3 "2024-08-16T12:07:44Z")

</div>

I think as @Sukera 's said that `==` falls back to `===` by default, and because **immutable** objects is stack allocated (share memory for performance) see this ⬇

```julia
julia> g1 |> objectid
0x00000000247a75a3

julia> g2 |> objectid
0x00000000247a75a3

```

But for **mutable** objects it is heap allocated (object created don’t share memory) ⬇

```julia
julia> f1 |> objectid
0xd8248564904acb41

julia> f2 |> objectid
0x088b6250e182dc49

```

Which is not the same objects so `===` and `==` returns `false` , But you can overload `==` with `Base.:(==)` like this

```julia
julia> Base.:(==)(f::Foo, f::Foo) = f.x == f.x

```

So `f1 == f2` returns `true`.
