Skip to content

Commit c7e7805

Browse files
authored
make IndexStyle compatible with Base and test inferrability (JuliaArrays#65)
2 parents 3b5d398 + 58697cf commit c7e7805

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/structarray.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ const StructVector{T, C<:NamedTuple} = StructArray{T, 1, C}
4242
StructVector{T}(args...; kwargs...) where {T} = StructArray{T}(args...; kwargs...)
4343
StructVector(args...; kwargs...) = StructArray(args...; kwargs...)
4444

45-
Base.IndexStyle(::Type{StructArray{T, N, C}}) where {T, N, C} = Base.IndexStyle(tuple_type(C).parameters[1])
45+
_indexstyle(::Type{Tuple{}}) = IndexStyle(Union{})
46+
_indexstyle(::Type{T}) where {T<:Tuple} = IndexStyle(IndexStyle(tuple_type_head(T)), _indexstyle(tuple_type_tail(T)))
47+
_indexstyle(::Type{NamedTuple{names, types}}) where {names, types} = _indexstyle(types)
48+
49+
function Base.IndexStyle(::Type{StructArray{T, N, C}}) where {T, N, C}
50+
_indexstyle(C)
51+
end
4652

4753
_undef_array(::Type{T}, sz; unwrap = t -> false) where {T} = unwrap(T) ? StructArray{T}(undef, sz; unwrap = unwrap) : Array{T}(undef, sz)
4854

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ end
1717
@test StructArrays.propertynames(StructArrays.fieldarrays(t)) == (:a, :b)
1818
end
1919

20+
@testset "indexstyle" begin
21+
@inferred IndexStyle(StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10)))
22+
T = typeof(StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10)))
23+
@test IndexStyle(T) === IndexCartesian()
24+
@inferred IndexStyle(StructArray(a=rand(10,10), b=rand(10,10)))
25+
T = typeof(StructArray(a=rand(10,10), b=rand(10,10)))
26+
@test IndexStyle(T) === IndexLinear()
27+
end
28+
2029
@testset "replace_storage" begin
2130
v = StructArray(a=rand(10), b = fill("string", 10))
2231
v_pooled = StructArrays.replace_storage(v) do c

0 commit comments

Comments
 (0)