Babypopo
Babypopo

Reputation: 151

How to initialize an array with NamedTuples in Julia 1.6.0?

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!

Upvotes: 3

Views: 732

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69839

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

Upvotes: 7

Related Questions