StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Allow `stack` on an empty collection of SArrays

Open jariji opened this issue 1 year ago • 1 comments

julia> stack(SVector{3,Int}[]; dims=1)
ERROR: ArgumentError: `stack` on an empty collection is not allowed

This could work and it would be convenient for handling the empty case. Thoughts?

jariji avatar Dec 03 '24 23:12 jariji

Xref https://github.com/JuliaLang/julia/issues/55735 about why this was removed from the original stack PR.

At an earlier point, it did infer things like stack(empty!([(1,2,3)])) to be 3×0 Matrix. The same could have worked for this Vector{SVector} example (not sure if it did). But these paths gave type-instabilities... where normally stack(xs) calls similar(xs[1], ...), the empty case can't do that & wanted methods of similar(eltype(xs), ...) which don't exist.

In the stacktrace from the above error, the last function is _empty_stack which used to implement this, and now just always throws. It might be possible for StaticArrays to overload this, to catch only types it owns & hence not produce instabilities on Base / StructArrays.

mcabbott avatar Dec 07 '24 16:12 mcabbott