# Where is the lowering of end within \[ \] documented

**URL:** https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736
**Category:** General Usage
**Tags:** question, indexing, lowering
**Created:** [April 16, 2026, 10:42am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736 "2026-04-16T10:42:22Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 16, 2026, 10:42am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/1 "2026-04-16T10:42:22Z")

</div>

Where is the lowering of `end` within `[]` _documented_? Ie the fact that it is assigned a value from `lastindex`, as in

```julia
julia> Meta.@lower (1:4)[min(end, 5)]
:($(Expr(:thunk, CodeInfo(
1 ─ %1 = Main.:(:)
│ %2 = dynamic (%1)(1, 4)
│ %3 = Main.min
│ %4 = dynamic Base.lastindex(%2)
│ %5 = dynamic (%3)(%4, 5)
│ %6 = dynamic Base.getindex(%2, %5)
└── return %6
))))

```

I just realized that I know this, but can’t find it in the docs (context: someone asked me about it and I wanted to share a docs section that explains it.)

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [April 16, 2026, 10:54am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/2 "2026-04-16T10:54:47Z")

</div>

I think it’s hidden away in the compiler internals.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 16, 2026, 11:13am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/3 "2026-04-16T11:13:29Z")

</div>

It’s documented in the `lastindex` docstring, which says:

> The syntaxes `A[end]` and `A[end, end]` lower to `A[lastindex(A)]` and `A[lastindex(A, 1), lastindex(A, 2)]`, respectively.

The `end` docstring also says:

> `end` may also be used when indexing to represent the last index of a collection or the last index of a dimension of an array.

but it should probably cross-reference `lastindex` specifically.

Probably it should also be referenced from the “Arrays” chapter of the manual, which currently only says:

> As a special part of this syntax, the `end` keyword may be used to represent the last index of each dimension within the indexing brackets, as determined by the size of the innermost array being indexed.

> <https://github.com/JuliaLang/julia/pull/61589>
>
> The fact that \`end\` in brackets lowers to a \`lastindex\` call was documented in t…he \`lastindex\` docstring, but was only alluded to more vaguely as the "last index" in other places, e.g. in the Arrays chapter of the manual or the \`end\` docstring. Similarly for \`begin\` and \`firstindex\`.
> 
> (Inspired by a \[discourse thread\](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736?u=stevengj).)

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [April 16, 2026, 12:17pm UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/5 "2026-04-16T12:17:41Z")

</div>

Funnily enough, this is done in the parser, not in lowering.

```julia-auto
julia> Meta.parse("[1][begin : end]")
:(([1])[var"begin":var"end"])

julia> Meta.parse("[begin : end]")
:([begin
          #= none:1 =#
          :
      end])

julia> [begin : end]
1-element Vector{Colon}:
 (::Colon) (generic function with 16 methods)

```

If you think about it for a second, then it is clear why this has to work that way: Macros.  
That is quite the footgun for code generation!

And the distinction is not “internals”, it is public API (macros). So I guess the docs should say `var"begin"` and `var"end"` get lowered to appropriate firstindex/lastindex called when within an indexing bracket; and `begin`/`end` sometimes get parsed into `var"begin"`/`var"end"` when within an indexing bracket.

…and the “sometimes” is really non-trivial – see `begin : end`, which plausibly could have been `getindex(A, Colon())` or `getindex(A, Colon()(firstindex(A), lastindex(A)))`. Luckily both give the same result for most (all?) Base types.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [April 16, 2026, 1:56pm UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/7 "2026-04-16T13:56:50Z")

</div>

It’s also documented on the `end` keyword itself!

```julia-auto
help?> end
search: end tand read secd sind nand rand bind

  end

  end marks the conclusion of a block of expressions, for example module,
  struct, mutable struct, begin, let, for etc.

  end may also be used when indexing to represent the last index of a
  collection or the last index of a dimension of an array.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> A = [1 2; 3 4]
  2×2 Matrix{Int64}:
   1 2
   3 4

  julia> A[end, :]
  2-element Vector{Int64}:
   3
   4

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 16, 2026, 2:11pm UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/8 "2026-04-16T14:11:09Z")

</div>

> [@mbauman](#):
>
> It’s also documented on the `end` keyword itself!

Yes, this is understood, but _what_ this feature does precisely is not documented there.

Some languages have similar keywords or concepts, but do not implement them in such a general way.

---

<div class="post-metadata">

### Author: ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)
#### Post date: [April 18, 2026, 1:38am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/9 "2026-04-18T01:38:24Z")

</div>

So I don’t think it is a good way to index the first and last elements. Mathematica’s 1 and -1 are quite concise, as long as you give up offset arrays.

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [April 18, 2026, 9:23am UTC](https://discourse.julialang.org/t/where-is-the-lowering-of-end-within-documented/136736/10 "2026-04-18T09:23:33Z")

</div>

> [@foobar\_lv2](#):
>
> ```julia-repl
> julia> Meta.parse("[1][begin : end]")
> :(([1])[var"begin":var"end"])
> 
> ```

Does this behavior depend on the Julia version or so? If I try this in 1.12.6 (and 1.10.11), it looks like this:

```julia-repl
julia> Meta.parse("[1][begin : end]")
:(([1])[begin:end])

```

`var"begin"` and `var"end"` don’t seem to exist for me.

PS: Or did you define them by hand to make a point? I’m not sure why anyone would do that though 😅 And even if I set `var"begin"` and `var"end"` to something, the above still parses to the same for me (no `var"begin"` just `begin`).
