Comparing mutable structs with identical field values in Julia

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

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> ==(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
2 Likes