using Pkg pkg"add StructEquality"
which gives you access to one macro
Motivation & Usage
Struct types have an
== implementation by default which uses
===, i.e. object identity, on the underlying
components to compare structs.
Let's define a struct
struct MyStruct a::Int b::Vector end
== fails to compare two structs with the same content
MyStruct(1, [2,3]) == MyStruct(1, [2,3]) # false
To fix this use the supplied macro
@def_structequal MyStruct MyStruct(1, [2,3]) == MyStruct(1, [2,3]) # true
Alternatively you can use the macro right on struct definition
@def_structequal struct MyStruct2 a::Int b::Vector end MyStruct2(1, [2,3]) == MyStruct2(1, [2,3]) # true
It is like you would expect. the macro extracts the field names and defines
== by referring to
of the fields.
@macroexpand @def_structequal MyStruct
:(function Base.:(==)(s1::MyStruct, s2::MyStruct) s1.a == s2.a && s1.b == s2.b end)
For more details to this topic, please see the discourse thread https://discourse.julialang.org/t/surprising-struct-equality-test/4890/9