# Different structs with the same properties

**URL:** <https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [September 30, 2023, 1:41am UTC](https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417 "2023-09-30T01:41:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 30, 2023, 1:41am UTC](https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417/1 "2023-09-30T01:41:24Z")

</div>

I have two .jld2 files.  
I loaded structs from the above files, which have the same properties but they are regarded as different objects as follows:

```julia
julia> for prop in [fieldnames(typeof(a))..., fieldnames(typeof(b))...,]
       if getproperty(a, prop) != getproperty(b, prop)
       error()
       end
       end

julia> a == b
false

```

I tried to reproduce the above phenomenon with a minimum example but it behaves in a different way like this:

```julia
julia> struct MyStruct
       a
       b
       end

julia> mine1 = MyStruct(1, 2)
MyStruct(1, 2)

julia> mine2 = MyStruct(1, 2)
MyStruct(1, 2)

julia> mine1 == mine2
true

```

I don’t get it so far.  
Anyone can explain this…?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 30, 2023, 7:26am UTC](https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417/2 "2023-09-30T07:26:33Z")

</div>

I think it is because the default implementation of `==` for a new `struct` falls back on `===`, which tests if the two objects share the same memory address (i.e. if they are one and the same). If your struct includes mutable fields, you need to redefine `Base.:(==)` for your own struct in a way that compares field values instead.

> <https://github.com/JuliaLang/julia/issues/4648>
>
> This doesn't make much sense:
> 
> \`\`\` julia
> julia\> immutable Foo{T}
> bar::T…
> end
> 
> julia\> Foo("baz") == Foo("baz")
> false
> 
> julia\> Foo("baz").bar == Foo("baz").bar
> true
> 
> julia\> Foo(1) == Foo(1)
> true
> \`\`\`
> 
> If the fields are \`==\` to each other then the objects should be \`==\` to each other.

> [@Surprising struct equality test](https://discourse.julialang.org/t/surprising-struct-equality-test/4890/):
>
> By my understanding of structs, they should be compared by value by default. So what is going here?? struct T name::String end T("hello") == T("hello") returns false!

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 30, 2023, 8:29am UTC](https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417/3 "2023-09-30T08:29:46Z")

</div>

Yeah that’s right. Here’s an example with the same struct `MyStruct` but now containing a mutable object (a `Vector`)

```julia
julia> mine1 = MyStruct([1, 2], 1);

julia> mine2 = MyStruct([1, 2], 1);

julia> mine1 == mine2
false

```

and you can see that equality fails.

@iHany you might find the package [AutoHashEquals.jl](https://github.com/JuliaServices/AutoHashEquals.jl) useful

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [October 1, 2023, 8:01am UTC](https://discourse.julialang.org/t/different-structs-with-the-same-properties/104417/4 "2023-10-01T08:01:08Z")

</div>

Thank you guys!!  
This helped me a lot in understanding the mechanism and why it happened.
