I’m getting some odd type instability when using the NamedTuple constructor
NamedTuple{symbol}(values)
Here’s a simple example:
using IteractiveUtils
names = (:x,:y)
vals = (rand(2,2), rand(2,2))
create_nt(names,vals) = NamedTuple{names}(vals)
create_nt(vals) = NamedTuple{(:x,:y)}(vals)
checking for type stability returns
julia> @code_warntype create_nt(names,vals)
Body::Any
1 ─ %1 = (Core.apply_type)(Main.NamedTuple, names)::Type{NamedTuple{_1,T} where T<:Tuple} where _1
│ %2 = (%1)(vals)::Any
└── return %2
julia> @code_warntype create_nt(vals)
Body::NamedTuple{(:x, :y),Tuple{Array{Float64,2},Array{Float64,2}}}
1 ─ %1 = (Core.getfield)(vals, 1)::Array{Float64,2}
│ %2 = (Core.getfield)(vals, 2)::Array{Float64,2}
│ %3 = %new(NamedTuple{(:x, :y),Tuple{Array{Float64,2},Array{Float64,2}}}, %1, %2)::NamedTuple{(:x, :y),Tuple{Array{Float64,2},Array{Float64,2}}}
└── return %3
This is very puzzling behavior to me. Why can’t I pass in an external tuple of symbols? This makes dynamically creating NamedTuples very difficult. Is there another constructor I should be using?
Thanks!
Running Julia 1.1 on Linux Mint