diff --git a/src/interface.jl b/src/interface.jl index 90422750..123dadcd 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -10,4 +10,5 @@ end staticschema(::Type{T}) where {T<:Tup} = T createinstance(::Type{T}, args...) where {T} = T(args...) -createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args) +createinstance(::Type{<:Tuple}, args...) = args +createinstance(::Type{<:NamedTuple{names}}, args...) where {names} = NamedTuple{names}(args) diff --git a/src/structarray.jl b/src/structarray.jl index f9b43c0a..308a94a5 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -134,11 +134,13 @@ Base.axes(s::StructArray) = axes(fieldarrays(s)[1]) Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,) get_ith(cols::NamedTuple, I...) = get_ith(Tuple(cols), I...) -function get_ith(cols::NTuple{N, Any}, I...) where N - ntuple(N) do i - @inbounds res = getfield(cols, i)[I...] - return res +@generated get_ith(cols::NTuple{N, Any}, I...) where N = _get_ith(N) +function _get_ith(N::Integer) + ex = Expr(:tuple) + for i=1:N + push!(ex.args, :(getfield(cols, $i)[I...])) end + ex end Base.@propagate_inbounds function Base.getindex(x::StructArray{T, <:Any, <:Any, CartesianIndex{N}}, I::Vararg{Int, N}) where {T, N}