Skip to content

Commit 7cb2582

Browse files
authored
Clarify axes return type. (#37852)
1. document that the indices cannot be arbitrary unit ranges (eg of unit, dates, etc). 2. add a cautionary note about custom indexes (ie current OffsetArrays behavior)
1 parent f8b9a52 commit 7cb2582

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

base/abstractarray.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,25 @@ Return the valid range of indices for array `A` along dimension `d`.
4545
See also [`size`](@ref), and the manual chapter on [arrays with custom indices](@ref man-custom-indices).
4646
4747
# Examples
48+
4849
```jldoctest
4950
julia> A = fill(1, (5,6,7));
5051
5152
julia> axes(A, 2)
5253
Base.OneTo(6)
5354
```
55+
56+
# Usage note
57+
58+
Each of the indices has to be an `AbstractUnitRange{<:Integer}`, but at the same time can be
59+
a type that uses custom indices. So, for example, if you need a subset, use generalized
60+
indexing constructs like `begin`/`end` or [`firstindex`](@ref)/[`lastindex`](@ref):
61+
62+
```julia
63+
ix = axes(v, 1)
64+
ix[2:end] # will work for eg Vector, but may fail in general
65+
ix[(begin+1):end] # works for generalized indexes
66+
```
5467
"""
5568
function axes(A::AbstractArray{T,N}, d) where {T,N}
5669
@_inline_meta
@@ -63,6 +76,7 @@ end
6376
Return the tuple of valid indices for array `A`.
6477
6578
# Examples
79+
6680
```jldoctest
6781
julia> A = fill(1, (5,6,7));
6882

doc/src/devdocs/offset-arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ the cause try running julia with the option `--check-bounds=yes`.)
5656

5757
### Using `axes` for bounds checks and loop iteration
5858

59-
`axes(A)` (reminiscent of `size(A)`) returns a tuple of `AbstractUnitRange` objects, specifying
59+
`axes(A)` (reminiscent of `size(A)`) returns a tuple of `AbstractUnitRange{<:Integer}` objects, specifying
6060
the range of valid indices along each dimension of `A`. When `A` has unconventional indexing,
6161
the ranges may not start at 1. If you just want the range for a particular dimension `d`, there
6262
is `axes(A, d)`.

doc/src/manual/interfaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ ourselves, we can officially define it as a subtype of an [`AbstractArray`](@ref
234234
| `similar(A, dims::Dims)` | `similar(A, eltype(A), dims)` | Return a mutable array with the same element type and size *dims* |
235235
| `similar(A, ::Type{S}, dims::Dims)` | `Array{S}(undef, dims)` | Return a mutable array with the specified element type and size |
236236
| **Non-traditional indices** | **Default definition** | **Brief description** |
237-
| `axes(A)` | `map(OneTo, size(A))` | Return the `AbstractUnitRange` of valid indices |
237+
| `axes(A)` | `map(OneTo, size(A))` | Return the a tuple of `AbstractUnitRange{<:Integer}` of valid indices |
238238
| `similar(A, ::Type{S}, inds)` | `similar(A, S, Base.to_shape(inds))` | Return a mutable array with the specified indices `inds` (see below) |
239239
| `similar(T::Union{Type,Function}, inds)` | `T(Base.to_shape(inds))` | Return an array similar to `T` with the specified indices `inds` (see below) |
240240

0 commit comments

Comments
 (0)