diff --git a/Project.toml b/Project.toml index 4534076..43acaff 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StructHelpers" uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f" authors = ["Jan Weidner and contributors"] -version = "0.1.4" +version = "0.1.5" [deps] ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" diff --git a/src/StructHelpers.jl b/src/StructHelpers.jl index 368b1ff..795044d 100644 --- a/src/StructHelpers.jl +++ b/src/StructHelpers.jl @@ -8,6 +8,9 @@ import ConstructionBase: getproperties, constructorof, setproperties @inline function structural_eq(o1, o2) getproperties(o1) == getproperties(o2) end +@inline function structural_isequal(o1, o2) + isequal(getproperties(o1), getproperties(o2)) +end start_hash(o, h, typesalt::Nothing) = Base.hash(typeof(o), h) start_hash(o, h, typesalt) = Base.hash(typesalt, h) @@ -41,7 +44,8 @@ function def_kwconstructor(T, propertynames) end const BATTERIES_DEFAULTS = ( - eq = true , + eq = true, + isequal = true, hash = true , kwconstructor = false, selfconstructor = true, @@ -53,6 +57,7 @@ const BATTERIES_DEFAULTS = ( const BATTERIES_DOCSTRINGS = ( eq = "Define `Base.(==)` structurally.", + isequal = "Define `Base.isequal` structurally.", hash = "Define `Base.hash` structurally.", kwconstructor = "Add a keyword constructor.", selfconstructor = "Add a constructor of the for `T(self::T) = self`", @@ -150,6 +155,10 @@ macro batteries(T, kw...) def = :(Base.:(==)(o1::$T, o2::$T) = $(structural_eq)(o1, o2)) push!(ret.args, def) end + if nt.isequal + def = :(Base.isequal(o1::$T, o2::$T) = $(structural_isequal)(o1, o2)) + push!(ret.args, def) + end if nt.kwshow def = :(Base.show(io::IO, o::$T) = $(kwshow)(io, o)) push!(ret.args, def) diff --git a/test/runtests.jl b/test/runtests.jl index 0d97e65..2491cb0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -43,6 +43,9 @@ struct WithSelfCtor; a; end @batteries NoSelfCtor selfconstructor=false @batteries WithSelfCtor selfconstructor=true +struct SNoIsEqual; a; end +@batteries SNoIsEqual isequal=false + @testset "@batteries" begin @test SBatteries(1,2) == SBatteries(1,2) @test SBatteries(1,[]) == SBatteries(1,[]) @@ -50,6 +53,12 @@ struct WithSelfCtor; a; end @test SNoHash(1,[]) == SNoHash(1,[]) @test SVanilla(1,[]) != SVanilla(1,[]) + @test isequal(SBatteries(NaN, 1), SBatteries(NaN, 1)) + @test !isequal(SBatteries(1, 1), SBatteries(NaN, 1)) + @test !isequal(SBatteries(NaN, 1), SBatteries(NaN, 2)) + @test SBatteries(NaN, 1) != SBatteries(NaN, 1) + @test !isequal(SNoIsEqual(NaN), SNoIsEqual(NaN)) + @test isequal(SNoIsEqual(1), SNoIsEqual(1)) @test SBatteries(2,1) != SBatteries(1,2) @test Skw(2,1) != Skw(1,2) @test SNoHash(2,1) != SNoHash(1,2)