# ranges and `end` token

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)

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?

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

5 Likes

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

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

4 Likes

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

5 Likes