Skip to content

Commit e4678ce

Browse files
authored
add some specialization hints for function arguments (#162)
merge the several `t -> false` functions into one
1 parent 2efa1f6 commit e4678ce

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/collect.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct StructArrayInitializer{F, G}
44
unwrap::F
55
arrayof::G
66
end
7-
StructArrayInitializer(unwrap = t->false) = StructArrayInitializer(unwrap, arrayof)
7+
StructArrayInitializer(unwrap::F = alwaysfalse) where {F} = StructArrayInitializer(unwrap, arrayof)
88

99
const default_initializer = StructArrayInitializer()
1010

@@ -17,7 +17,7 @@ struct ArrayInitializer{F, G}
1717
unwrap::F
1818
arrayof::G
1919
end
20-
ArrayInitializer(unwrap = t->false) = ArrayInitializer(unwrap, arrayof)
20+
ArrayInitializer(unwrap::F = alwaysfalse) where {F} = ArrayInitializer(unwrap, arrayof)
2121

2222
(s::ArrayInitializer)(S, d) = s.unwrap(S) ? buildfromschema(typ -> s(typ, d), S) : s.arrayof(S, d)
2323

src/structarray.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ function Base.IndexStyle(::Type{S}) where {S<:StructArray}
113113
index_type(S) === Int ? IndexLinear() : IndexCartesian()
114114
end
115115

116-
function _undef_array(::Type{T}, sz; unwrap = t -> false) where {T}
116+
function _undef_array(::Type{T}, sz; unwrap::F = alwaysfalse) where {T, F}
117117
if unwrap(T)
118118
return StructArray{T}(undef, sz; unwrap = unwrap)
119119
else
120120
return Array{T}(undef, sz)
121121
end
122122
end
123123

124-
function _similar(v::AbstractArray, ::Type{Z}; unwrap = t -> false) where {Z}
124+
function _similar(v::AbstractArray, ::Type{Z}; unwrap::F = alwaysfalse) where {Z, F}
125125
if unwrap(Z)
126126
return buildfromschema(typ -> _similar(v, typ; unwrap = unwrap), Z)
127127
else
@@ -149,12 +149,12 @@ julia> StructArray{ComplexF64}(undef, (2,3))
149149
"""
150150
StructArray(::Base.UndefInitializer, sz::Dims)
151151

152-
function StructArray{T}(::Base.UndefInitializer, sz::Dims; unwrap = t -> false) where {T}
152+
function StructArray{T}(::Base.UndefInitializer, sz::Dims; unwrap::F = alwaysfalse) where {T, F}
153153
buildfromschema(typ -> _undef_array(typ, sz; unwrap = unwrap), T)
154154
end
155-
StructArray{T}(u::Base.UndefInitializer, d::Integer...; unwrap = t -> false) where {T} = StructArray{T}(u, convert(Dims, d); unwrap = unwrap)
155+
StructArray{T}(u::Base.UndefInitializer, d::Integer...; unwrap::F = alwaysfalse) where {T, F} = StructArray{T}(u, convert(Dims, d); unwrap = unwrap)
156156

157-
function similar_structarray(v::AbstractArray, ::Type{Z}; unwrap = t -> false) where {Z}
157+
function similar_structarray(v::AbstractArray, ::Type{Z}; unwrap::F = alwaysfalse) where {Z, F}
158158
buildfromschema(typ -> _similar(v, typ; unwrap = unwrap), Z)
159159
end
160160

@@ -203,11 +203,11 @@ julia> StructArray((1, Complex(i, j)) for i = 1:3, j = 2:4; unwrap = T -> !(T<:R
203203
(1, 3+2im) (1, 3+3im) (1, 3+4im)
204204
```
205205
"""
206-
function StructArray(v; unwrap = t -> false)::StructArray
206+
function StructArray(v; unwrap::F = alwaysfalse)::StructArray where {F}
207207
collect_structarray(v; initializer = StructArrayInitializer(unwrap))
208208
end
209209

210-
function StructArray(v::AbstractArray{T}; unwrap = t -> false) where {T}
210+
function StructArray(v::AbstractArray{T}; unwrap::F = alwaysfalse) where {T, F}
211211
s = similar_structarray(v, T; unwrap = unwrap)
212212
for i in eachindex(v)
213213
@inbounds s[i] = v[i]

src/utils.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
eltypes(::Type{T}) where {T} = map_params(eltype, T)
22

3+
alwaysfalse(t) = false
4+
35
"""
46
StructArrays.map_params(f, T)
57
@@ -33,10 +35,10 @@ _map_params(f, ::Type{Tuple{}}) = ()
3335
function _map_params(f, ::Type{T}) where {T<:Tuple}
3436
(f(tuple_type_head(T)), _map_params(f, tuple_type_tail(T))...)
3537
end
36-
_map_params(f, ::Type{NamedTuple{names, types}}) where {names, types} =
38+
_map_params(f::F, ::Type{NamedTuple{names, types}}) where {names, types, F} =
3739
NamedTuple{names}(_map_params(f, types))
3840

39-
buildfromschema(initializer, ::Type{T}) where {T} = buildfromschema(initializer, T, staticschema(T))
41+
buildfromschema(initializer::F, ::Type{T}) where {T, F} = buildfromschema(initializer, T, staticschema(T))
4042

4143
"""
4244
StructArrays.buildfromschema(initializer, T[, S])
@@ -47,7 +49,7 @@ Construct a [`StructArray{T}`](@ref) with a function `initializer`, using a sche
4749
4850
`S` is a `Tuple` or `NamedTuple` type. The default value is [`staticschema(T)`](@ref).
4951
"""
50-
function buildfromschema(initializer, ::Type{T}, ::Type{NT}) where {T, NT<:Tup}
52+
function buildfromschema(initializer::F, ::Type{T}, ::Type{NT}) where {T, NT<:Tup, F}
5153
nt = _map_params(initializer, NT)
5254
StructArray{T}(nt)
5355
end

0 commit comments

Comments
 (0)