@@ -5,6 +5,8 @@ eltypes(::Type{T}) where {T<:Tuple} =
55 tuple_type_cons (eltype (tuple_type_head (T)), eltypes (tuple_type_tail (T)))
66eltypes (:: Type{NamedTuple{K, V}} ) where {K, V} = eltypes (V)
77
8+ Base. @pure SkipConstructor (:: Type ) = false
9+
810@generated function get_ith (s:: StructArray{T} , I... ) where {T}
911 args = []
1012 for key in fields (T)
3133 end
3234end
3335
34- createinstance (:: Type{T} , args... ) where {T} = T (args... )
36+ function createinstance (:: Type{T} , args... ) where {T}
37+ SkipConstructor (T) ? unsafe_createinstance (T, args... ) : T (args... )
38+ end
39+
3540createinstance (:: Type{T} , args... ) where {T<: Union{Tuple, NamedTuple} } = T (args)
3641
42+ @generated function unsafe_createinstance (:: Type{T} , args... ) where {T}
43+ v = fieldnames (T)
44+ new_tup = Expr (:(= ), Expr (:tuple , v... ), :args )
45+ construct = Expr (:new , :T , (:(convert (fieldtype (T, $ (Expr (:quote , sym))), $ sym)) for sym in v). .. )
46+ Expr (:block , new_tup, construct)
47+ end
48+
3749createtype (:: Type{T} , :: Type{C} ) where {T<: NamedTuple{N} , C} where {N} = NamedTuple{N, C}
3850createtype (:: Type{T} , :: Type{C} ) where {T, C} = T
0 commit comments