Skip to content

Commit 8a5ac94

Browse files
authored
make firstindex(a,d) and lastindex(a,d) default to calling axes(a,d) (#38742)
1 parent e585557 commit 8a5ac94

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Language changes
4444
a function like other operators. The dotted version `.-->` is now parsed as well.
4545
For backwards compatibility, `-->` still parses using its own expression head
4646
instead of `:call`.
47-
* The `a[begin]` syntax now calls `firstindex(a,1)` rather than `first(axes(a,1))` ([#35779]).
47+
* The `a[begin, k]` syntax now calls `firstindex(a, 1)` rather than `first(axes(a, 1))` ([#35779]), but the former now defaults to the latter for any `a` ([#38742]).
4848
* `` (U+233F) and `¦` (U+00A6) are now infix operators with times-like and plus-like precedence,
4949
respectively. Previously they were parsed as identifier characters ([#37973]).
5050

base/abstractarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ julia> lastindex(rand(3,4,5), 2)
345345
```
346346
"""
347347
lastindex(a::AbstractArray) = (@_inline_meta; last(eachindex(IndexLinear(), a)))
348-
lastindex(a::AbstractArray, d) = (@_inline_meta; last(axes(a, d)))
348+
lastindex(a, d) = (@_inline_meta; last(axes(a, d)))
349349

350350
"""
351351
firstindex(collection) -> Integer
@@ -363,7 +363,7 @@ julia> firstindex(rand(3,4,5), 2)
363363
```
364364
"""
365365
firstindex(a::AbstractArray) = (@_inline_meta; first(eachindex(IndexLinear(), a)))
366-
firstindex(a::AbstractArray, d) = (@_inline_meta; first(axes(a, d)))
366+
firstindex(a, d) = (@_inline_meta; first(axes(a, d)))
367367

368368
first(a::AbstractArray) = a[first(eachindex(a))]
369369

doc/src/manual/interfaces.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ julia> Squares(23)[end]
193193
529
194194
```
195195

196+
For multi-dimensional `begin`/`end` indexing as in `a[3, begin, 7]`, for example,
197+
you should define `firstindex(a, dim)` and `lastindex(a, dim)`
198+
(which default to calling `first` and `last` on `axes(a, dim)`, respectively).
199+
196200
Note, though, that the above *only* defines [`getindex`](@ref) with one integer index. Indexing with
197201
anything other than an `Int` will throw a [`MethodError`](@ref) saying that there was no matching method.
198202
In order to support indexing with ranges or vectors of `Int`s, separate methods must be written:

0 commit comments

Comments
 (0)