Suppose I have an array
a = [1, 2, 3, 4, 5].
I can index into it at a single value:
a == 2. I can index it with a range:
a[2:4] == [2, 3, 4]. I can even get the whole array back with
a[:] == a.
Where this pattern breaks down is when writing a one-sided range:
a[3:] != a[3:5] == [3, 4, 5].
Yes, we have
end for this purpose, and as of v1.4,
begin for ranges starting at the beginning of an index.
But is there a technical reason why syntax like
a[3:] can’t be translated to
[3, 4, 5]? Or similarly, why
a[:3] can’t become
a[begin:3] == [1, 2, 3]? This feels cleaner, and is similar to Python slice syntax.
This syntax would even more useful in multidimensional arrays, while remaining unambiguous (as far as I can tell). So instead of
b[3:end, begin:4], you’d have
a[3:] yields a range syntax error:
ERROR: syntax: missing last argument in "3:" range expression
a[:3] is interpreted as
a. While this is valid code, I don’t believe that anyone would intentionally write this expecting to get
Would it be possible to integrate this syntax into Julia without breaking any existing code or adding too much parse logic?