How to initialize an array with NamedTuples in Julia 1.6.0?

Viewed 344

I can initialize an array of tuples with Array{Tuple{Int, Int}}(undef, 10).

However, I cannot make it work for named tuples. Does anyone know how I can do this?

Thanks in advance!

1 Answers

This is the way to do it (I understand you want an uninitialized array of NamedTuples having a concrete type):

julia> Array{NamedTuple{(:a, :b), Tuple{Int64, Float64}}}(undef, 10)
10-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Float64}}}:
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 2.156699073e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 2.15670018e-315)
 (a = 257472272, b = 1.272082044e-315)
 (a = 257472272, b = 1.272082044e-315)

julia> Array{NamedTuple{(:a, :b), Tuple{Int64, String}}}(undef, 10)
10-element Vector{NamedTuple{(:a, :b), Tuple{Int64, String}}}:
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
Related