# Define equality for struct by checking all fields

**URL:** <https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325>\
**Category:** New to Julia\
**Tags:** struct, meta\
**Created:** [October 15, 2024, 11:09am UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325 "2024-10-15T11:09:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [October 15, 2024, 11:09am UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325/1 "2024-10-15T11:09:28Z")

</div>

I find myself in the position of defining various structs of the form

```julia
struct Foo{T, U}
 bar::T
 bar2::U
end

```

where I want to define Foo(a,b) == Foo(c,d) iff a == c and b == d. I can do this manually:

```julia
Base.:(==)(op1::Foo, op2::Foo) = op1.bar == op2.bar && op1.bar2 == op2.bar2

```

However, if I have many structs, each with several fields, this becomes a bit tedious. Is there a way to do this automatically through a macro or something?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 15, 2024, 11:35am UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325/2 "2024-10-15T11:35:22Z")

</div>

No built-in way unfortunately ([see this issue](https://github.com/JuliaLang/julia/issues/53565)).  
Use a package, eg [GitHub - jw3126/StructHelpers.jl: Automate common struct definition boilerplate](https://github.com/jw3126/StructHelpers.jl) :

```julia
using StructHelpers

struct S
    a
    b
end

@batteries S

```

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [October 15, 2024, 11:47am UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325/3 "2024-10-15T11:47:01Z")

</div>

That is a bit inconvenient indeed, you’d expect there to be some base macro for this. Thanks for the pointer!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 15, 2024, 12:27pm UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325/4 "2024-10-15T12:27:40Z")

</div>

You can also do this, depending on the complexity of the structures:

```julia-repl
julia> struct A
           x
           y
       end

julia> a = A(1,2); b = A(1,2)
A(1, 2)

julia> import Base: ==

julia> ==(a::A,b::A) = all(getfield(a,f) == getfield(b,f) for f in fieldnames(A))
== (generic function with 198 methods)

julia> a == b
true

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 15, 2024, 3:47pm UTC](https://discourse.julialang.org/t/define-equality-for-struct-by-checking-all-fields/121325/5 "2024-10-15T15:47:45Z")

</div>

An alternative, using @lmiq’s example:

```julia
getfield.(Ref(a), fieldnames(A)) == getfield.(Ref(b), fieldnames(A))

```
