1- eltypes ( :: Type{T} ) where {T} = map_params (eltype, T)
1+ argtail (_, args ... ) = args
22
3- alwaysfalse (t) = false
4-
5- """
6- StructArrays.map_params(f, T)
7-
8- Apply `f` to each field type of `Tuple` or `NamedTuple` type `T`, returning a
9- new `Tuple` or `NamedTuple` type.
3+ split_tuple_type (T) = fieldtype (T, 1 ), Tuple{argtail (T. parameters... )... }
104
11- ```julia-repl
12- julia> StructArrays.map_params(T -> Complex{T}, Tuple{Int32,Float64})
13- Tuple{Complex{Int32},Complex{Float64}}
14- ```
15- """
16- map_params (f, :: Type{NamedTuple{names, types}} ) where {names, types} =
17- NamedTuple{names, map_params (f, types)}
5+ eltypes (nt:: NamedTuple{names} ) where {names} = NamedTuple{names, eltypes (values (nt))}
6+ eltypes (t:: Tuple ) = Tuple{map (eltype, t)... }
187
19- function map_params (f, :: Type{T} ) where {T<: Tuple }
20- if @generated
21- types = fieldtypes (T)
22- ex = :(Tuple{})
23- for t ∈ types
24- push! (ex. args, :(f ($ t)))
25- end
26- ex
27- else
28- map_params_fallback (f, T)
29- end
30- end
31-
32- map_params_fallback (f, :: Type{T} ) where {T<: Tuple } = Tuple{map (f, fieldtypes (T))... }
8+ alwaysfalse (t) = false
339
3410"""
35- StructArrays._map_params (f, T)
11+ StructArrays.map_params (f, T)
3612
3713Apply `f` to each field type of `Tuple` or `NamedTuple` type `T`, returning a
3814new `Tuple` or `NamedTuple` object.
3915
4016```julia-repl
41- julia> StructArrays._map_params (T -> Complex{T}, Tuple{Int32,Float64})
17+ julia> StructArrays.map_params (T -> Complex{T}, Tuple{Int32,Float64})
4218(Complex{Int32}, Complex{Float64})
4319```
4420"""
45- _map_params (f:: F , :: Type{NamedTuple{names, types}} ) where {names, types, F } =
46- NamedTuple {names} (_map_params (f, types))
21+ map_params (f:: F , :: Type{NamedTuple{names, types}} ) where {F, names, types } =
22+ NamedTuple {names} (map_params (f, types))
4723
48- function _map_params (f:: F , :: Type{T} ) where {T<: Tuple , F }
24+ function map_params (f:: F , :: Type{T} ) where {F, T<: Tuple }
4925 if @generated
5026 types = fieldtypes (T)
51- ex = :()
52- for t ∈ types
53- push! (ex. args, :(f ($ t)))
54- end
55- ex
27+ args = map (t -> :(f ($ t)), types)
28+ Expr (:tuple , args... )
5629 else
57- _map_params_fallback (f, T)
30+ map_params_fallback (f, T)
5831 end
5932end
6033
61- _map_params_fallback (f, :: Type{T} ) where {T<: Tuple } = map (f, fieldtypes (T))
34+ map_params_fallback (f, :: Type{T} ) where {T<: Tuple } = map (f, fieldtypes (T))
6235
63- buildfromschema (initializer:: F , :: Type{T} ) where {T, F } = buildfromschema (initializer, T, staticschema (T))
36+ buildfromschema (initializer:: F , :: Type{T} ) where {F, T } = buildfromschema (initializer, T, staticschema (T))
6437
6538"""
6639 StructArrays.buildfromschema(initializer, T[, S])
@@ -71,8 +44,8 @@ Construct a [`StructArray{T}`](@ref) with a function `initializer`, using a sche
7144
7245`S` is a `Tuple` or `NamedTuple` type. The default value is [`staticschema(T)`](@ref).
7346"""
74- function buildfromschema (initializer:: F , :: Type{T} , :: Type{NT} ) where {T, NT<: Tup , F }
75- nt = _map_params (initializer, NT)
47+ function buildfromschema (initializer:: F , :: Type{T} , :: Type{NT} ) where {F, T, NT<: Tup }
48+ nt = map_params (initializer, NT)
7649 StructArray {T} (nt)
7750end
7851
@@ -123,16 +96,16 @@ iscompatible(::Type{Tuple{}}, ::Type{T}) where {T<:Tuple} = false
12396iscompatible (:: Type{T} , :: Type{Tuple{}} ) where {T<: Tuple } = false
12497iscompatible (:: Type{Tuple{}} , :: Type{Tuple{}} ) = true
12598
126- function iscompatible (:: Type{S} , :: Type{T} ) where {S<: Tuple , T<: Tuple }
127- iscompatible (tuple_type_head (S), tuple_type_head (T)) && iscompatible (tuple_type_tail (S), tuple_type_tail (T))
99+ function iscompatible (:: Type{T} , :: Type{T′} ) where {T<: Tuple , T′<: Tuple }
100+ (f, ls), (f′, ls′) = split_tuple_type (T), split_tuple_type (T′)
101+ iscompatible (f, f′) && iscompatible (ls, ls′)
128102end
129103
130- iscompatible (:: S , :: T ) where {S, T <: AbstractArray } = iscompatible (S, T )
104+ iscompatible (:: S , :: V ) where {S, V <: AbstractArray } = iscompatible (S, V )
131105
132- function _promote_typejoin (:: Type{S} , :: Type{T} ) where {S<: NTuple{N, Any} , T<: NTuple{N, Any} } where N
133- head = _promote_typejoin (Base. tuple_type_head (S), Base. tuple_type_head (T))
134- tail = _promote_typejoin (Base. tuple_type_tail (S), Base. tuple_type_tail (T))
135- return Base. tuple_type_cons (head, tail)
106+ function _promote_typejoin (:: Type{T} , :: Type{T′} ) where {T<: NTuple{N, Any} , T′<: NTuple{N, Any} } where N
107+ (f, ls), (f′, ls′) = split_tuple_type (T), split_tuple_type (T′)
108+ return Tuple{_promote_typejoin (f, f′), _promote_typejoin (ls, ls′). parameters... }
136109end
137110
138111_promote_typejoin (:: Type{Tuple{}} , :: Type{Tuple{}} ) = Tuple{}
@@ -141,7 +114,7 @@ function _promote_typejoin(::Type{NamedTuple{names, types}}, ::Type{NamedTuple{n
141114 return NamedTuple{names, T}
142115end
143116
144- _promote_typejoin (:: Type{S } , :: Type{T} ) where {S , T} = Base. promote_typejoin (S , T)
117+ _promote_typejoin (:: Type{T } , :: Type{T′ } ) where {T , T′ } = Base. promote_typejoin (T , T′ )
145118
146119function _promote_typejoin (:: Type{Pair{A, B}} , :: Type{Pair{A′, B′}} ) where {A, A′, B, B′}
147120 C = _promote_typejoin (A, A′)
0 commit comments