Parameters.jl
Parameters.jl copied to clipboard
Nested structs fails
Hey, I have stumbled upon the following problem in Julia 1.1, here is a MWE:
julia> using Parameters
julia> @with_kw struct Foo; foo; end
Foo
julia> Foo(foo=2)
Foo
foo: Int64 2
julia> Foo(foo=Foo(foo=2))
Foo
foo: Int64 2
julia> Foo(foo=2) == Foo(foo=Foo(foo=2))
true
julia> Foo(2) == Foo(Foo(2))
true
Using standard structs
julia> struct Bar; bar; end
julia> Bar(2) == Bar(Bar(2))
false
Using Base.@kwdef in Julia 1.1
julia> Base.@kwdef struct Baz; baz; end
Baz
julia> Baz(baz=2) == Baz(baz=Baz(baz=2))
false
julia> Baz(2) == Baz(Baz(2))
false
I have no idea why this happens, but it looks like a bug.
Yes, I think this is https://github.com/JuliaLang/julia/pull/29316#issuecomment-424096502. Note that with a two-field struct it works ok:
julia> @with_kw struct Bar; a; b; end
Bar
julia> Bar(Bar(1,1), 99).a
Bar
a: Int64 1
b: Int64 1
Maybe the re-construct constructor should be dropped in favor of Setfield.jl or just mutating for mutable.
Seems to be right. It is definitely an unexpected behavior. In my use case I had something like a graph, specifying parent nodes. Having Node(parent=Node(parent=RootNode())) == Node(parent=RootNode()) is simply bamboozling. I actually switched to Base.@kwdef to remove this issue.
I guess the original way I thought about Parameters is that such a type would contain lots of fields, so this did not occur to me...