Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested structs fails #96

Open
cedeerwe opened this issue Mar 11, 2019 · 3 comments
Open

Nested structs fails #96

cedeerwe opened this issue Mar 11, 2019 · 3 comments

Comments

@cedeerwe
Copy link

cedeerwe commented Mar 11, 2019

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.

@mauro3
Copy link
Owner

mauro3 commented Mar 11, 2019

Yes, I think this is JuliaLang/julia#29316 (comment). 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.

@cedeerwe
Copy link
Author

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.

@mauro3
Copy link
Owner

mauro3 commented Mar 12, 2019

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants