Array indexing with begin

I ran into the following (Julia v1.5.3):

julia> A = rand(2,2);

julia> Meta.@lower A[end,1]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.lastindex(A, 1)
│   %2 = Base.getindex(A, %1, 1)
└──      return %2
))))

julia> Meta.@lower A[begin,1]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.axes(A, 1)
│   %2 = Base.first(%1)
│   %3 = Base.getindex(A, %2, 1)
└──      return %3
))))

It seems that when using two indices for a matrix begin and end lowers to different implementations. Is there any reason for this? (I would have expected A[begin,1] to call Base.firstindex(A, i) the same way end calls Base.lastindex(A, i).

Note that if we simply use A[begin] then it calls Base.firstindex(A) (and similarly A[end] calls Base.lastindex(A)).

1 Like

This was changed in https://github.com/JuliaLang/julia/pull/35779, which will be in 1.6.

2 Likes

Ah I actually tried to search for an Issue before asking but my GitHub search skills are not so good :slight_smile: