Skip to content

Commit 74f26f6

Browse files
jishnubnsajkomateuszbaran
authored
SOneTo subtypes AbstractOneTo (#1302)
On a recent nightly (v"1.13.0-DEV.347"), `Base` has added `AbstractOneTo` (JuliaLang/julia#56902), and in this PR we change `SOneTo` to subtype `AbstractOneTo` when this is available. This makes combinations of `SOneTo`, `Base.OneTo` and `Integer`s dispatch to `Base` methods, and we don't need to disambiguate these in this package. Fixes #1248. After this, `StaticArrays` only needs to define `similar` methods for `Tuple`s of `SOneTo` as indices. --------- Co-authored-by: Neven Sajko <4944410+nsajko@users.noreply.github.com> Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent 0d68e03 commit 74f26f6

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/SOneTo.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
const SOneToSupertype = isdefined(Base, :AbstractOneTo) ? Base.AbstractOneTo : AbstractUnitRange
2+
13
"""
24
SOneTo(n)
35
46
Return a statically-sized `AbstractUnitRange` starting at `1`, functioning as the `axes` of
57
a `StaticArray`.
68
"""
7-
struct SOneTo{n} <: AbstractUnitRange{Int}
9+
struct SOneTo{n} <: SOneToSupertype{Int}
810
end
911

1012
SOneTo(n::Int) = SOneTo{n}()

src/abstractarray.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ const HeterogeneousBaseShape = Union{Integer, Base.OneTo}
142142
const HeterogeneousShape = Union{HeterogeneousBaseShape, SOneTo}
143143
const HeterogeneousShapeTuple = Tuple{Vararg{HeterogeneousShape}}
144144

145-
similar(A::AbstractArray, ::Type{T}, shape::HeterogeneousShapeTuple) where {T} = similar(A, T, homogenize_shape(shape))
146-
similar(::Type{A}, shape::HeterogeneousShapeTuple) where {A<:AbstractArray} = similar(A, homogenize_shape(shape))
145+
@static if isdefined(Base, :AbstractOneTo)
146+
similar(A::AbstractArray, ::Type{T}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {T} = similar(A, T, homogenize_shape(shape))
147+
similar(::Type{A}, shape::Tuple{SOneTo, Vararg{SOneTo}}) where {A<:AbstractArray} = similar(A, homogenize_shape(shape))
148+
else
149+
similar(A::AbstractArray, ::Type{T}, shape::HeterogeneousShapeTuple) where {T} = similar(A, T, homogenize_shape(shape))
150+
similar(::Type{A}, shape::HeterogeneousShapeTuple) where {A<:AbstractArray} = similar(A, homogenize_shape(shape))
151+
end
147152
# Use an Array for StaticArrays if we don't have a statically-known size
148153
similar(::Type{A}, shape::Tuple{Int, Vararg{Int}}) where {A<:StaticArray} = Array{eltype(A)}(undef, shape)
149154

0 commit comments

Comments
 (0)