# Comparing mutable structs with identical field values in Julia

**URL:** <https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586>\
**Category:** General Usage\
**Tags:** mutable-structure, julia, comparison\
**Created:** [July 3, 2024, 12:52pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586 "2024-07-03T12:52:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![baiango](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baiango/32/210434_2.png) [@baiango](https://discourse.julialang.org/u/baiango)\
**Post date:** [July 3, 2024, 12:52pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/1 "2024-07-03T12:52:19Z")

</div>

## Description

I am working on a project that involves comparing mutable structs with identical field values in Julia. However, I am encountering unexpected results when using the `==` and `===` operators, as they return false for mutable structs with identical field values. According to the Julia documentation, the `==` operator falls back to `===` for structs.

## Here’s a minimal reproducible example:

```jl
mutable struct MutRange
	start::Int
	finish::Int
end

println(MutRange(1, 3) === MutRange(1, 3)) # false
println(MutRange(1, 3) == MutRange(1, 3)) # false

```

Based on the documentation at [Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.:==), the `==` operator falls back to `===` for structs. However, in this case, both comparisons return false.

This issue is reported at [GitHub issues](https://github.com/JuliaLang/julia/issues/55007).

Could someone please provide guidance on how to compare mutable structs correctly with identical field values in Julia? I appreciate any help and expertise you can provide.

Thank you in advance for your assistance!

---

<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:** [July 3, 2024, 1:00pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/2 "2024-07-03T13:00:12Z")

</div>

One way is to define your custom comparison operator, for your type. This is probably the safest way:

```julia-repl
julia> mutable struct MutRange
               start::Int
               finish::Int
       end

julia> import Base: ==

julia> ==(x::MutRange, y::MutRange) = x.start == y.start && x.finish == y.finish
== (generic function with 195 methods)

julia> MutRange(1,3) == MutRange(1,3)
true

```

You can make this more general with:

```julia-repl
julia> ==(x::MutRange, y::MutRange) = all(getfield(x, field) == getfield(y, field) for field in fieldnames(MutRange))
== (generic function with 195 methods)

julia> MutRange(1,3) == MutRange(1,3)
true

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 3, 2024, 1:00pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/3 "2024-07-03T13:00:42Z")

</div>

`===` checks object identity. So

```julia
x = MutRange(1, 3)
y = MutRange(1, 3)

```

Then `x === x` and `y === y` are true, but `x === y` is false. If you haven’t defined `==` it will just call `===`

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [July 3, 2024, 1:01pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/4 "2024-07-03T13:01:21Z")

</div>

If you want to overload equality to fit your needs there are various packages for this, see [GitHub - jw3126/StructHelpers.jl: Automate common struct definition boilerplate](https://github.com/jw3126/StructHelpers.jl)

If you don’t want to overload equality, you can do:

```julia
using ConstructionBase
getproperties(obj1) == getproperties(obj2)

```

---

<div class="post-metadata">

**Author:** ![baiango](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baiango/32/210434_2.png) [@baiango](https://discourse.julialang.org/u/baiango)\
**Post date:** [July 3, 2024, 1:07pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/5 "2024-07-03T13:07:47Z")

</div>

Thank you for your helpful suggestion! Defining a custom comparison operator for my `MutRange` struct solved the issue. Here’s my updated code:

```jl
mutable struct MutRange
	start::Int
	finish::Int
end

import Base: ==

==(x::MutRange, y::MutRange) = x.start == y.start && x.finish == y.finish

println(MutRange(1, 3) === MutRange(1, 3)) # false
println(MutRange(1, 3) == MutRange(1, 3)) # true

```

This custom comparison operator ensures that mutable structs with identical field values are correctly compared. I appreciate your expertise and assistance in resolving this problem.

Alternatively, you can define a function like this:

```jl
import Base: ==

function ==(a::MutRange, b::MutRange)
	return a.start == b.start && a.finish == b.finish
end

```

This function can be used to compare `MutRange` instances similarly to the custom comparison operator. The custom comparison operator is defined as an anonymous function within the `import Base: ==` statement, which may be unfamiliar to new Julia users. The alternative function provides a more explicit way to define the comparison operator.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 3, 2024, 3:03pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586/6 "2024-07-03T15:03:26Z")

</div>

Maybe it is too much for this case, but I like this package for this: [GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash() to composite types.](https://github.com/JuliaServices/AutoHashEquals.jl)
