jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
287 stars 34 forks source link

Indexing over zero-length lists or arrays fails #174

Closed nrontsis closed 1 year ago

nrontsis commented 1 year ago
using ComponentArrays
a = ComponentVector(;a=1, b=2)

julia> a[()]
ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
  [1] reduce_empty(op::Base.BottomRF{typeof(vcat)}, #unused#::Type{Union{}})
    @ Base ./reduce.jl:355
  [2] reduce_empty_iter(op::Base.BottomRF{typeof(vcat)}, itr::Tuple{}, #unused#::Base.HasEltype)
    @ Base ./reduce.jl:379
  [3] reduce_empty_iter(op::Base.BottomRF{typeof(vcat)}, itr::Tuple{})
    @ Base ./reduce.jl:378
  [4] foldl_impl(op::Base.BottomRF{typeof(vcat)}, nt::Base._InitialValue, itr::Tuple{})
    @ Base ./reduce.jl:49
  [5] mapfoldl_impl(f::typeof(identity), op::typeof(vcat), nt::Base._InitialValue, itr::Tuple{})
    @ Base ./reduce.jl:44
  [6] mapfoldl(f::Function, op::Function, itr::Tuple{}; init::Base._InitialValue)
    @ Base ./reduce.jl:170
  [7] mapfoldl
    @ ./reduce.jl:170 [inlined]
  [8] #mapreduce#263
    @ ./reduce.jl:302 [inlined]
  [9] mapreduce
    @ ./reduce.jl:302 [inlined]
 [10] #reduce#265
    @ ./reduce.jl:483 [inlined]
 [11] reduce(op::Function, itr::Tuple{})
    @ Base ./reduce.jl:483
 [12] getindex(ax::Axis{(a = 1, b = 2)}, syms::Tuple{})
    @ ComponentArrays ~/.julia/packages/ComponentArrays/YyD7i/src/axis.jl:173
 [13] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
 [14] _broadcast_getindex
    @ ./broadcast.jl:643 [inlined]
 [15] (::Base.Broadcast.var"#29#30"{Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(getindex), Tuple{Tuple{Axis{(a = 1, b = 2)}}, Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(ComponentArrays.getval), Tuple{Tuple{DataType}}}}}})(k::Int64)
    @ Base.Broadcast ./broadcast.jl:1075
 [16] ntuple
    @ ./ntuple.jl:48 [inlined]
 [17] copy
    @ ./broadcast.jl:1075 [inlined]
 [18] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(getindex), Tuple{Tuple{Axis{(a = 1, b = 2)}}, Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(ComponentArrays.getval), Tuple{Tuple{DataType}}}}})
    @ Base.Broadcast ./broadcast.jl:860
 [19] #s15#55
    @ ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:122 [inlined]
 [20] var"#s15#55"(::Any, index_fun::Any, x::Any, idx::Any)
    @ ComponentArrays ./none:0
 [21] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:582
 [22] getindex
    @ ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:105 [inlined]
 [23] getindex(x::ComponentVector{Int64, Vector{Int64}, Tuple{Axis{(a = 1, b = 2)}}}, idx::Tuple{})
    @ ComponentArrays ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:104
 [24] top-level scope
    @ REPL[9]:1

julia> a[Vector{Symbol}()]
ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
  [1] reduce_empty(op::Base.BottomRF{typeof(vcat)}, #unused#::Type{Union{}})
    @ Base ./reduce.jl:355
  [2] reduce_empty_iter(op::Base.BottomRF{typeof(vcat)}, itr::Tuple{}, #unused#::Base.HasEltype)
    @ Base ./reduce.jl:379
  [3] reduce_empty_iter(op::Base.BottomRF{typeof(vcat)}, itr::Tuple{})
    @ Base ./reduce.jl:378
  [4] foldl_impl(op::Base.BottomRF{typeof(vcat)}, nt::Base._InitialValue, itr::Tuple{})
    @ Base ./reduce.jl:49
  [5] mapfoldl_impl(f::typeof(identity), op::typeof(vcat), nt::Base._InitialValue, itr::Tuple{})
    @ Base ./reduce.jl:44
  [6] mapfoldl(f::Function, op::Function, itr::Tuple{}; init::Base._InitialValue)
    @ Base ./reduce.jl:170
  [7] mapfoldl
    @ ./reduce.jl:170 [inlined]
  [8] #mapreduce#263
    @ ./reduce.jl:302 [inlined]
  [9] mapreduce
    @ ./reduce.jl:302 [inlined]
 [10] #reduce#265
    @ ./reduce.jl:483 [inlined]
 [11] reduce(op::Function, itr::Tuple{})
    @ Base ./reduce.jl:483
 [12] getindex(ax::Axis{(a = 1, b = 2)}, syms::Tuple{})
    @ ComponentArrays ~/.julia/packages/ComponentArrays/YyD7i/src/axis.jl:173
 [13] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
 [14] _broadcast_getindex
    @ ./broadcast.jl:643 [inlined]
 [15] (::Base.Broadcast.var"#29#30"{Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(getindex), Tuple{Tuple{Axis{(a = 1, b = 2)}}, Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(ComponentArrays.getval), Tuple{Tuple{DataType}}}}}})(k::Int64)
    @ Base.Broadcast ./broadcast.jl:1075
 [16] ntuple
    @ ./ntuple.jl:48 [inlined]
 [17] copy
    @ ./broadcast.jl:1075 [inlined]
 [18] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(getindex), Tuple{Tuple{Axis{(a = 1, b = 2)}}, Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(ComponentArrays.getval), Tuple{Tuple{DataType}}}}})
    @ Base.Broadcast ./broadcast.jl:860
 [19] #s15#55
    @ ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:122 [inlined]
 [20] var"#s15#55"(::Any, index_fun::Any, x::Any, idx::Any)
    @ ComponentArrays ./none:0
 [21] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:582
 [22] getindex
    @ ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:105 [inlined]
 [23] getindex(x::ComponentVector{Int64, Vector{Int64}, Tuple{Axis{(a = 1, b = 2)}}}, idx::Vector{Symbol})
    @ ComponentArrays ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:104
 [24] top-level scope
    @ REPL[11]:1

julia> a[[]]
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Vector{Any}
Stacktrace:
  [1] Val(x::Vector{Any})
    @ Base ./essentials.jl:714
  [2] toval
    @ ~/.julia/packages/ComponentArrays/YyD7i/src/utils.jl:6 [inlined]
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:643 [inlined]
  [5] #29
    @ ./broadcast.jl:1075 [inlined]
  [6] ntuple
    @ ./ntuple.jl:48 [inlined]
  [7] copy
    @ ./broadcast.jl:1075 [inlined]
  [8] materialize
    @ ./broadcast.jl:860 [inlined]
  [9] getindex(x::ComponentVector{Int64, Vector{Int64}, Tuple{Axis{(a = 1, b = 2)}}}, idx::Vector{Any})
    @ ComponentArrays ~/.julia/packages/ComponentArrays/YyD7i/src/array_interface.jl:104
 [10] top-level scope
    @ REPL[10]:1

The first two can be fixed by changing reduce(vcat, inds) to vcat(inds) here. But then there is type instability in that a normal vector is returned back (related to #173).

nrontsis commented 1 year ago

Closed via https://github.com/jonniedie/ComponentArrays.jl/pull/180