Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would rather prefer that functions in base like broadcast never needed to call this function in this (second/negative) case... I.e. If custom types neglect to implement indexing operations other than linear or exactly N dimensional Cartesian, then it would be great if everything respected that and just worked (this surprised me in some point in the past).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would indeed simplify things. It's what I'm doing right now as a workaround. So far I noticed this breaks display(A), but I'm sure that's fixable.

end

"""
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))