Hi,
I am trying to write a method for finalizing incomplete initialization of a struct. I want to avoid using a mutable struct.
I found Setfield.jl or Accessors.jl which seem to fulfill my requirements, but it fails somehow.
Is there a simple fix for this or is there an alternative ?
Here is a mwe.jl:
using Accessors
struct Foo{T, N}
a::Array{T, N}
b::Int
# incomplete ctor & finalize
Foo(a::Array{T, N}) where {T, N} = begin
println("incomplete")
this = new{T, N}(a)
return complete(this)
end
# forward ctor
Foo(a::Array{T, N}, args...) where {T, N} = begin
println("forward $(args)")
return new{T, N}(a, args...)
end
end
complete(f::Foo{T, N}) where {T, N} = begin
@set f.b = 100
@show f.b # <== something is wrong
return f
end
function main()
@show Foo(ones(Float32, (1, 1)), 50) # OK
@show Foo(ones(Float32, (1, 2))) # NOK
return
end
main()