Updating structs with type variables
artemsolod opened this issue · 3 comments
artemsolod commented
Hi!
Thank you for this great package!
I've noticed that having type variable in a struct breaks things down.
struct A{T}
x :: Int
end
a = A{:Foo}(1)
@set a.x = 2
ERROR: MethodError: no method matching A(::Int64)
Stacktrace:
[1] macro expansion at /home/artem/.julia/packages/ConstructionBase/BMpJA/src/ConstructionBase.jl:0 [inlined]
[2] setproperties at /home/artem/.julia/packages/ConstructionBase/BMpJA/src/ConstructionBase.jl:47 [inlined]
[3] macro expansion at /home/artem/.julia/packages/Setfield/n9pIa/src/lens.jl:104 [inlined]
[4] set(::A{:Foo}, ::Setfield.PropertyLens{:x}, ::Int64) at /home/artem/.julia/packages/Setfield/n9pIa/src/lens.jl:104
[5] top-level scope at /home/artem/.julia/packages/Setfield/n9pIa/src/sugar.jl:166
This may lead to unexpected behaviour, if we had some additional constructor, e.g.
A(x :: Int) = (println("OMG OMG"); A{:Bar}(x))
results in
julia> @set a.x = 2
OMG OMG
A{:Bar}(2)
Although I might be misusing the package. Is there a workaround to handle type variables correctly?
jw3126 commented
You are not misusing the package. I recommend to design A
as described here. If that is not an option, consider overloading ConstructionBase.setproperties
.
jw3126 commented
@artemsolod is the issue resolved for you?
artemsolod commented
@jw3126, I understand this is expected behavior so I guess the issue is resolved, thank you!