# Comparing Mutable Structs

**URL:** <https://discourse.julialang.org/t/comparing-mutable-structs/10944>\
**Category:** General Usage\
**Created:** [May 16, 2018, 11:12pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944 "2018-05-16T23:12:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 16, 2018, 11:12pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/1 "2018-05-16T23:12:01Z")

</div>

dear wizards—is this intended behavior?

```julia
julia> mutable struct S; x::Int; end#struct##

julia> v= [S(1), S(2)]
2-element Array{S,1}:
 S(1)
 S(2)

julia> v==deepcopy(v) ## only when v is mutable
false

julia> v==copy(v)
true

```

if so, how do I check that a deepcopy() result has identical content to its original?

advice appreciated.

regards,

/iaw

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 16, 2018, 11:38pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/2 "2018-05-16T23:38:26Z")

</div>

This is fully intended. You should note that `v = S(1); v != deepcopy(v)` and `S(1) != S(1)` so comparing the object with `!=` won’t be useful.

By definition you can check the serialization being the same, though you should really specify what do you actually want to do. Writing test for `deepcopy`?

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 17, 2018, 12:31am UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/3 "2018-05-17T00:31:55Z")

</div>

uggh. I should have started simpler. My premise was wrong. I have a worse lack of understanding mutables vs const than I thought. It does way more than just make contents unchangeable.

```julia
julia> mutable struct S; x::Int; end#struct##

julia S(1) == S(1)
false

```

I had known that `S(1) === S(1)`, because the left object is distinguishable from the right object. The L creates an object different from the R, so assigning to one would not be the same as assigning to the other.

So what is `S(1) == S(1)` actually testing that is different from `S(1) === S(2)` with mutable structs?

```julia
julia> struct R; x::Int; end#struct##

julia R(1) == R(1)
true

```

Similar question here for immutable. Again, `==` and `===` seem to do the same thing.

It is a virtue when programming languages give the naively expected results…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 17, 2018, 1:02am UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/4 "2018-05-17T01:02:26Z")

</div>

> [@iwelch](#):
>
> So what is `S(1) == S(1)` actually testing that is different from `S(1) === S(2)` with mutable structs?

By default, nothing. You can check this yourself. Just do:

```julia
julia> mutable struct S; end

julia> @edit S() == S()

```

which shows that the default implementation is:

```julia
## generic comparison ##

==(x, y) = x === y

```

The difference is that you can overload `==` to mean whatever you want for a particular type.

There’s no way for Julia to know exactly what `==` equality means to you for some new type, so by default it does the most conservative thing and only declares two mutable structs as equal if they are truly indistinguishable.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 17, 2018, 3:25am UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/5 "2018-05-17T03:25:10Z")

</div>

And note that if you do overload `Base.:(==)`, [you should also overload `Base.hash`](https://discourse.julialang.org/t/how-to-use-unique-function/5266) (except you should overload the two-argument version, as noted in [https://docs.julialang.org/en/stable/stdlib/base/#Base.hash](https://docs.julialang.org/en/stable/stdlib/base/#Base.hash)); see [https://docs.julialang.org/en/stable/stdlib/base/#Base.isequal-Tuple{Any,Any}](https://docs.julialang.org/en/stable/stdlib/base/#Base.isequal-Tuple%7BAny,Any%7D).

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 17, 2018, 7:31am UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/6 "2018-05-17T07:31:57Z")

</div>

Related reading: [https://github.com/JuliaLang/julia/issues/4648](https://github.com/JuliaLang/julia/issues/4648)

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 17, 2018, 10:02pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/7 "2018-05-17T22:02:14Z")

</div>

> [@rdeits](#):
>
> There’s no way for Julia to know exactly what `==` equality means to you for some new type,

even C knows this for structs! 😉

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 17, 2018, 10:36pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/8 "2018-05-17T22:36:38Z")

</div>

> [@iwelch](#):
>
> even C knows this for structs! 😉

Are you sure about that?

- [How do you compare structs for equality in C? - Stack Overflow](https://stackoverflow.com/a/141724/641846)
- [https://www.quora.com/Why-cant-I-directly-compare-two-structure-types-variables-in-C](https://www.quora.com/Why-cant-I-directly-compare-two-structure-types-variables-in-C)
- [standards - Why are structs not allowed in equality expressions in C? - Stack Overflow](https://stackoverflow.com/questions/46995631/why-are-structs-not-allowed-in-equality-expressions-in-c)

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 17, 2018, 11:55pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/9 "2018-05-17T23:55:02Z")

</div>

I stand corrected.

It just seems strange that if I can write `a=b`, I should be able to write `a==b` and (except for NaN type cases) receive true.

regards,

/iaw

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 18, 2018, 12:01am UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/10 "2018-05-18T00:01:21Z")

</div>

> [@iwelch](#):
>
> It just seems strange that if I can write `a=b` , I should be able to write `a==b` and (except for NaN type cases) receive true.

Note that this **IS** actually the case in julia.

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 18, 2018, 5:26pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/11 "2018-05-18T17:26:38Z")

</div>

this is what you get if you allow idiots onto discourse forums…

PS: fortunately, my turning red is not visible over the net.

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [February 2, 2020, 5:59pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs/10944/12 "2020-02-02T17:59:26Z")

</div>

> [@tkoolen](#):
>
> [https://docs.julialang.org/en/stable/stdlib/base/#Base.hash](https://docs.julialang.org/en/stable/stdlib/base/#Base.hash)); see [https://docs.julialang.org/en/stable/stdlib/base/#Base.isequal-Tuple{Any,Any}](https://docs.julialang.org/en/stable/stdlib/base/#Base.isequal-Tuple%7BAny,Any%7D).

The new links for these are:  
[https://docs.julialang.org/en/v1/base/base/#Base.hash](https://docs.julialang.org/en/v1/base/base/#Base.hash)  
[https://docs.julialang.org/en/v1/base/base/#Base.isequal](https://docs.julialang.org/en/v1/base/base/#Base.isequal)
