diff --git a/base/abstractarray.jl b/base/abstractarray.jl index bb5dfa258feaf..8e0fdb5bc5ce2 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -37,7 +37,7 @@ Base.OneTo(6) """ function indices(A::AbstractArray{T,N}, d) where {T,N} @_inline_meta - d <= N ? indices(A)[d] : OneTo(1) + d <= N ? indices(A)[d] : one(indicestype(typeof(A),d)) end """ @@ -57,6 +57,23 @@ function indices(A) map(OneTo, size(A)) end +""" + indicestype(T, d) + +Returns the type of the indices for array type `T` along dimension `d` + +```jldoctest +julia> A = ones(5,6,7); + +julia> indicestype(typeof(A), 1) +Base.OneTo{Int} +``` +""" +function indicestype{T <: AbstractArray}(::Type{T}, d) + @_inline_meta + OneTo{Int} +end + # Performance optimization: get rid of a branch on `d` in `indices(A, # d)` for d=1. 1d arrays are heavily used, and the first dimension # comes up in other applications. diff --git a/base/range.jl b/base/range.jl index 3736f721bf597..47b312f33cef8 100644 --- a/base/range.jl +++ b/base/range.jl @@ -897,3 +897,5 @@ function +(r1::StepRangeLen{T,S}, r2::StepRangeLen{T,S}) where {T,S} end -(r1::StepRangeLen, r2::StepRangeLen) = +(r1, -r2) + +one{T, RangeT <: Range{T}}(::Type{RangeT}) = RangeT(one(T))