Skip to content

Commit 7819e3a

Browse files
tkfc42f
andcommitted
Unpirate Union{}[] (#685)
* Duplicate SA[] function signatures to avoid Union{} special case * Test that Union{}[] and friends don't hit the StaticArrays code paths Co-Authored-By: Chris Foster <chris42f@gmail.com>
1 parent cb46c2d commit 7819e3a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/initializers.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ const SA_F64 = SA{Float64}
2626
@inline similar_type(::Type{SA}, ::Size{S}) where {S} = SArray{Tuple{S...}}
2727
@inline similar_type(::Type{SA{T}}, ::Size{S}) where {T,S} = SArray{Tuple{S...}, T}
2828

29-
@inline Base.getindex(sa::Type{<:SA}, xs...) = similar_type(sa, Size(length(xs)))(xs)
30-
@inline Base.typed_vcat(sa::Type{<:SA}, xs::Number...) = similar_type(sa, Size(length(xs)))(xs)
31-
@inline Base.typed_hcat(sa::Type{<:SA}, xs::Number...) = similar_type(sa, Size(1,length(xs)))(xs)
29+
# These definitions are duplicated to avoid matching `sa === Union{}` in the
30+
# neater-looking alternative `sa::Type{<:SA}`.
31+
@inline Base.getindex(sa::Type{SA}, xs...) = similar_type(sa, Size(length(xs)))(xs)
32+
@inline Base.getindex(sa::Type{SA{T}}, xs...) where T = similar_type(sa, Size(length(xs)))(xs)
33+
34+
@inline Base.typed_vcat(sa::Type{SA}, xs::Number...) = similar_type(sa, Size(length(xs)))(xs)
35+
@inline Base.typed_vcat(sa::Type{SA{T}}, xs::Number...) where T = similar_type(sa, Size(length(xs)))(xs)
36+
37+
@inline Base.typed_hcat(sa::Type{SA}, xs::Number...) = similar_type(sa, Size(1,length(xs)))(xs)
38+
@inline Base.typed_hcat(sa::Type{SA{T}}, xs::Number...) where T = similar_type(sa, Size(1,length(xs)))(xs)
3239

3340
Base.@pure function _SA_hvcat_transposed_size(rows)
3441
M = rows[1]
@@ -40,7 +47,7 @@ Base.@pure function _SA_hvcat_transposed_size(rows)
4047
Size(M, length(rows))
4148
end
4249

43-
@inline function Base.typed_hvcat(sa::Type{<:SA}, rows::Dims, xs::Number...)
50+
@inline function _SA_typed_hvcat(sa, rows, xs)
4451
msize = _SA_hvcat_transposed_size(rows)
4552
if msize === nothing
4653
throw(ArgumentError("SA[...] matrix rows of length $rows are inconsistent"))
@@ -49,3 +56,6 @@ end
4956
transpose(similar_type(sa, msize)(xs))
5057
end
5158

59+
@inline Base.typed_hvcat(sa::Type{SA}, rows::Dims, xs::Number...) = _SA_typed_hvcat(sa, rows, xs)
60+
@inline Base.typed_hvcat(sa::Type{SA{T}}, rows::Dims, xs::Number...) where T = _SA_typed_hvcat(sa, rows, xs)
61+

test/initializers.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ SA_test_hvcat(x,T) = SA{T}[1 x x;
3636
@test SA_F64[1, 2] === SVector{2,Float64}((1,2))
3737
@test SA_F32[1, 2] === SVector{2,Float32}((1,2))
3838

39+
# https://github.com/JuliaArrays/StaticArrays.jl/pull/685
40+
@test Union{}[] isa Vector{Union{}}
41+
@test Base.typed_vcat(Union{}) isa Vector{Union{}}
42+
@test Base.typed_hcat(Union{}) isa Vector{Union{}}
43+
@test Base.typed_hvcat(Union{}, ()) isa Vector{Union{}}
44+
@test_throws MethodError Union{}[1]
45+
@test_throws MethodError Union{}[1 2]
46+
@test_throws MethodError Union{}[1; 2]
47+
@test_throws MethodError Union{}[1 2; 3 4]
48+
3949
end

0 commit comments

Comments
 (0)