diff --git a/src/structarray.jl b/src/structarray.jl index a4c5acba..7c679a3a 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -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("at least one component array is required") + 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 @@ -91,9 +90,10 @@ julia> StructArray(a=zeros(2,2), b=ones(2,2)) """ StructArray(tup::Union{Tuple,NamedTuple}) -function StructArray{T}(c::C) where {T, C<:Tup} +function StructArray{T}(c::Tup) where {T} cols = strip_params(staticschema(T))(c) - N = isempty(cols) ? 1 : ndims(cols[1]) + isempty(cols) && error("at least one component array is required") + N = ndims(first(cols)) StructArray{T, N, typeof(cols)}(cols) end @@ -333,9 +333,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...) diff --git a/test/runtests.jl b/test/runtests.jl index aabf8c90..9111dd65 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -433,6 +433,14 @@ end t = StructVector([(a=1,), (a=missing,)])::StructVector @test isequal(t.a, [1, missing]) @test eltype(t) <: NamedTuple{(:a,)} + + @test_throws ErrorException StructArray([nothing]) + @test_throws ErrorException StructArray([1, 2, 3]) + @test_throws ErrorException StructArray{Nothing}(()) + @test_throws ErrorException StructArray{Tuple{}}(()) + NT = NamedTuple{(), Tuple{}} + @test_throws ErrorException StructArray{NT}((a=rand(10),)) + @test_throws ErrorException StructArray{Tuple{}, 1, Tuple{}}(()) end @testset "tuple case" begin