# ranges and \`end\` token

**URL:** <https://discourse.julialang.org/t/ranges-and-end-token/7422>\
**Category:** Internals & Design\
**Created:** [November 30, 2017, 9:16pm UTC](https://discourse.julialang.org/t/ranges-and-end-token/7422 "2017-11-30T21:16:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![undef](https://avatars.discourse-cdn.com/v4/letter/u/7feea3/32.png) [@undef](https://discourse.julialang.org/u/undef)\
**Post date:** [November 30, 2017, 9:16pm UTC](https://discourse.julialang.org/t/ranges-and-end-token/7422/1 "2017-11-30T21:16:12Z")

</div>

**TL;DR** Can we get rid of the `end` token in favor of negative indices, and can we display empty rages consistently?

I wondered why the end token is necessary, instead of just using negative indices.  
This was probably asked before, but it’s a difficult search term and not explained in the docs as far as I noticed.  
(I think negative indices would be more consistent, because Julia’s indices start at 1 by default, so reverse indices should start at (-)1 as well instead of 0 (end - 0).)

So I thought, it might be because it conflicts with ranges with negative indices, so I played around with that.  
I noticed that a range where the second number is smaller than the first always returns a range where the stop value is one smaller.  
E.g. `5:2`, `5:-1` all return `5:4`.  
Is that intended?  
(see [https://github.com/JuliaLang/julia/blob/f383276c5b91d4d111f628c2eaa6b2db2f496421/base/range.jl#L155-L156](https://github.com/JuliaLang/julia/blob/f383276c5b91d4d111f628c2eaa6b2db2f496421/base/range.jl#L155-L156))

Chris Rackauckas told me on Gitter, that they’re displayed identically, because they’re all the empty range, but I didn’t find that very intuitive… maybe it would be useful to have a standard way of displaying an empty range regardless of its indices?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 30, 2017, 9:30pm UTC](https://discourse.julialang.org/t/ranges-and-end-token/7422/2 "2017-11-30T21:30:51Z")

</div>

One reason for `end` is to allow concise index maths, e.g. `v[1:end÷2]` vs `v[1:length(v)÷2]`.

---

<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:** [November 30, 2017, 10:52pm UTC](https://discourse.julialang.org/t/ranges-and-end-token/7422/3 "2017-11-30T22:52:17Z")

</div>

It’s possible in Julia to have arrays whose indices include negative values:

> **[GitHub - JuliaArrays/OffsetArrays.jl: Fortran-like arrays with arbitrary,...](https://github.com/JuliaArrays/OffsetArrays.jl)**
>
> Fortran-like arrays with arbitrary, zero or negative starting indices. - GitHub - JuliaArrays/OffsetArrays.jl: Fortran-like arrays with arbitrary, zero or negative starting indices.

In such cases, `end` still works but negative indices could not be used to index from the end.

---

<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:** [November 30, 2017, 10:59pm UTC](https://discourse.julialang.org/t/ranges-and-end-token/7422/4 "2017-11-30T22:59:05Z")

</div>

Negative indices for reversal would also add the overhead of a sign check to every single indexing expression. Doesn’t matter in Python because scalar code is slow there anyway, but in Julia…

See also [Redirecting to Google Groups](https://groups.google.com/forum/m/#!msg/julia-dev/QMSgkl-gTnM/03aZkZDkY2sJ)
