I often want to do the following for a large n where I perform some calculations and return a tuple of values with the result of each iteration:
function f(n::Int)
v = Vector()
for i in 1:n
...
push!(v, (a=i, b=1.23, c=false, d=:foo, ...))
end
v
end
As written, the code is less efficient than it could be because v will have type Array{Any,1}. The type instability can be fixed by explicitly specifying the type of the NamedTuple, but that is cumbersome as the tuple’s size grows.
Is there any way to tell Julia that I only want to push a single type to the Array so that it can either
- automatically infer the type from the
push!, or - wait to initiate the
Arrayuntil the firstpush!call so that the type will be known at that time?