Skip to content

Commit d14604c

Browse files
authored
Merge pull request #549 from JuliaArrays/teh/fix_548
Support Base.Slice axes (fixes #548)
2 parents 4f089af + bb4aca8 commit d14604c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/abstractarray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ similar(::Type{A}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {A<:AbstractArray
126126
similar(::A,::Type{T}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {A<:AbstractArray,T} = similar(A, T, Size(last.(shape)))
127127
similar(::Type{A},::Type{T}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {A<:AbstractArray,T} = similar(A, T, Size(last.(shape)))
128128

129+
const SOneToLike{n} = Union{SOneTo{n}, Base.Slice{SOneTo{n}}}
130+
deslice(ax::SOneTo) = ax
131+
deslice(ax::Base.Slice) = ax.indices
132+
similar(::A,::Type{T}, shape::Tuple{SOneToLike, Vararg{SOneToLike}}) where {A<:AbstractArray,T} = similar(A, T, Size(last.(deslice.(shape))))
133+
similar(::Type{A},::Type{T}, shape::Tuple{SOneToLike, Vararg{SOneToLike}}) where {A<:AbstractArray,T} = similar(A, T, Size(last.(deslice.(shape))))
134+
129135
# Handle mixtures of SOneTo and other ranges (probably should make Base more robust here)
130136
similar(::Type{A}, shape::Tuple{AbstractUnitRange, Vararg{AbstractUnitRange}}) where {A<:AbstractArray} = similar(A, length.(shape)) # Jumps back to 2-argument form in Base
131137
similar(::Type{A},::Type{T}, shape::Tuple{AbstractUnitRange, Vararg{AbstractUnitRange}}) where {A<:AbstractArray,T} = similar(A, length.(shape))

test/abstractarray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ using StaticArrays, Test, LinearAlgebra
6868
@test isa(@inferred(similar(Matrix{Int}, Int, Size(2,2))), SizedArray{Tuple{2, 2}, Int, 2, 2})
6969
end
7070

71+
@testset "similar and Base.Slice (issue #548)" begin
72+
v = @SVector [1]
73+
b = similar(v, Int, Base.Slice(SOneTo(3)))
74+
@test b isa MVector{3,Int}
75+
end
76+
7177
@testset "reshape" begin
7278
@test @inferred(reshape(SVector(1,2,3,4), Size(2,2))) === SMatrix{2,2}(1,2,3,4)
7379
@test @inferred(reshape([1,2,3,4], Size(2,2)))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]

0 commit comments

Comments
 (0)