Unexpected additional behavior when using zip to construct NamedTuple

The issue is coming from fill which doesn’t instantiate copies, but fills the container with references to the same input object.

julia> x = fill(zeros(3),2)
2-element Vector{Vector{Float64}}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

julia> x[1] === x[2]
true

julia> x[1][1] = 1.0
1.0

julia> x
2-element Vector{Vector{Float64}}:
 [1.0, 0.0, 0.0]
 [1.0, 0.0, 0.0]

You can get the expected behavior with map

julia> test = (; zip((:a, :b), map(_ -> zeros(3), 1:2))...)
(a = [0.0, 0.0, 0.0], b = [0.0, 0.0, 0.0])

julia> test.a[1] = 1.0
1.0

julia> test
(a = [1.0, 0.0, 0.0], b = [0.0, 0.0, 0.0])
1 Like