Reputation: 43147
I was writing some code that makes use of array views and slices, but encountered some inconsistencies.
Why does the following not cause an exception:
a = [1,2]
@show a[3:end]
@show a[4:end]
The above all return empty arrays as expected
But this causes a BoundsError
a = [1,2]
@show a[2:3]
Why is the first index of the slice allowed to be larger than the size of the array itself, but Julia seems to have a problem with the last index being larger than the size?
Julia version: 1.3.1
Upvotes: 3
Views: 155
Reputation: 6976
x[c:end]
is syntax for getindex(x, UnitRange(c, lastindex(x)))
.
Any range a:b
with a > b
is empty. Indexing an array with an empty range will result in an empty array by definition of getindex
.
You index an array with an empty range in your first set of examples. In your second set of examples, you index with an out-of-bounds range, which errors as expected.
Upvotes: 4