1
- eltypes ( :: Type{T} ) where {T} = map_params (eltype, T)
1
+ argtail (_, args ... ) = args
2
2
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... )... }
10
4
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)... }
18
7
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
33
9
34
10
"""
35
- StructArrays._map_params (f, T)
11
+ StructArrays.map_params (f, T)
36
12
37
13
Apply `f` to each field type of `Tuple` or `NamedTuple` type `T`, returning a
38
14
new `Tuple` or `NamedTuple` object.
39
15
40
16
```julia-repl
41
- julia> StructArrays._map_params (T -> Complex{T}, Tuple{Int32,Float64})
17
+ julia> StructArrays.map_params (T -> Complex{T}, Tuple{Int32,Float64})
42
18
(Complex{Int32}, Complex{Float64})
43
19
```
44
20
"""
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))
47
23
48
- function _map_params (f:: F , :: Type{T} ) where {T<: Tuple , F }
24
+ function map_params (f:: F , :: Type{T} ) where {F, T<: Tuple }
49
25
if @generated
50
26
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... )
56
29
else
57
- _map_params_fallback (f, T)
30
+ map_params_fallback (f, T)
58
31
end
59
32
end
60
33
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))
62
35
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))
64
37
65
38
"""
66
39
StructArrays.buildfromschema(initializer, T[, S])
@@ -71,8 +44,8 @@ Construct a [`StructArray{T}`](@ref) with a function `initializer`, using a sche
71
44
72
45
`S` is a `Tuple` or `NamedTuple` type. The default value is [`staticschema(T)`](@ref).
73
46
"""
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)
76
49
StructArray {T} (nt)
77
50
end
78
51
@@ -123,16 +96,16 @@ iscompatible(::Type{Tuple{}}, ::Type{T}) where {T<:Tuple} = false
123
96
iscompatible (:: Type{T} , :: Type{Tuple{}} ) where {T<: Tuple } = false
124
97
iscompatible (:: Type{Tuple{}} , :: Type{Tuple{}} ) = true
125
98
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′)
128
102
end
129
103
130
- iscompatible (:: S , :: T ) where {S, T <: AbstractArray } = iscompatible (S, T )
104
+ iscompatible (:: S , :: V ) where {S, V <: AbstractArray } = iscompatible (S, V )
131
105
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... }
136
109
end
137
110
138
111
_promote_typejoin (:: Type{Tuple{}} , :: Type{Tuple{}} ) = Tuple{}
@@ -141,7 +114,7 @@ function _promote_typejoin(::Type{NamedTuple{names, types}}, ::Type{NamedTuple{n
141
114
return NamedTuple{names, T}
142
115
end
143
116
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′ )
145
118
146
119
function _promote_typejoin (:: Type{Pair{A, B}} , :: Type{Pair{A′, B′}} ) where {A, A′, B, B′}
147
120
C = _promote_typejoin (A, A′)
0 commit comments