Skip to content

Commit 01dd3e3

Browse files
authored
throw if no fields (#235)
* throw if no fields * after code review
1 parent 70a743e commit 01dd3e3

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/structarray.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
1414
components::C
1515

1616
function StructArray{T, N, C}(c) where {T, N, C<:Tup}
17-
if length(c) > 0
18-
ax = axes(first(c))
19-
length(ax) == N || error("wrong number of dimensions")
20-
map(tail(c)) do ci
21-
axes(ci) == ax || error("all field arrays must have same shape")
22-
end
17+
isempty(c) && error("Only eltypes with fields are supported")
18+
ax = axes(first(c))
19+
length(ax) == N || error("wrong number of dimensions")
20+
map(tail(c)) do ci
21+
axes(ci) == ax || error("all field arrays must have same shape")
2322
end
2423
new{T, N, C, index_type(c)}(c)
2524
end
@@ -333,9 +332,7 @@ staticschema(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = staticschema(
333332
createinstance(::Type{<:StructArray{T}}, args...) where {T} = StructArray{T}(args)
334333

335334
Base.size(s::StructArray) = size(components(s)[1])
336-
Base.size(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (0,)
337335
Base.axes(s::StructArray) = axes(components(s)[1])
338-
Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,)
339336

340337
"""
341338
StructArrays.get_ith(cols::Union{Tuple,NamedTuple}, I...)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ end
433433
t = StructVector([(a=1,), (a=missing,)])::StructVector
434434
@test isequal(t.a, [1, missing])
435435
@test eltype(t) <: NamedTuple{(:a,)}
436+
437+
@test_throws Exception StructArray([nothing])
438+
@test_throws Exception StructArray([1, 2, 3])
436439
end
437440

438441
@testset "tuple case" begin

0 commit comments

Comments
 (0)