Skip to content

Commit

Permalink
throw if no fields (#235)
Browse files Browse the repository at this point in the history
* throw if no fields

* after code review
  • Loading branch information
aplavin authored Sep 5, 2022
1 parent 70a743e commit 01dd3e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
components::C

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

Base.size(s::StructArray) = size(components(s)[1])
Base.size(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (0,)
Base.axes(s::StructArray) = axes(components(s)[1])
Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,)

"""
StructArrays.get_ith(cols::Union{Tuple,NamedTuple}, I...)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ end
t = StructVector([(a=1,), (a=missing,)])::StructVector
@test isequal(t.a, [1, missing])
@test eltype(t) <: NamedTuple{(:a,)}

@test_throws Exception StructArray([nothing])
@test_throws Exception StructArray([1, 2, 3])
end

@testset "tuple case" begin
Expand Down

0 comments on commit 01dd3e3

Please sign in to comment.