Julia's history: array indexing

I just noticed that xs[length(xs)+1+n:end] returns an empty vector instead of throwing a bounds error. I think this is brilliant!

When was this behavior introduced? I don’t think I’ve seen this behavior in other languages. I’m curious if there was a discussion around this? Maybe a PR?

AFAIK, it’s always been that way. length(xs)+1+n:end is just syntax for length(xs)+1+n:lastindex(xs), and ranges that end before they start are empty, so the getindex function returns immediately.

3 Likes

Ah, that makes a lot of sense, thanks for explaining. You all rock!

The earliest manual I can find online is v0.3.12, and it already talks about empty ranges like n:n-1.

I also remember liking this choice very much, after R’s

> 3:2
[1] 3 2

which demands that one special-cases these in many loops.

3 Likes