Use of "end" in array element selection

The end token means “last element of array”, and it is essentially equivalent to -1 in Python. However, that is not always true. Consider the array:

a = collection(1:100)
b = view(a, 1:20)
c = view(a, 30:end)

I get the error:

ERROR: syntax: missing last argument in "30:" range expression 

Why was the decision taken not to use the end token consistently? Why allow it in an expression a[20:end] and not in view(a,20:end)? I know I can use view(a, 20:length(a)), but that is a heavy notation, and Julia prides itself in being terse and elegant.

It’s because as the argument of a function, the expression 30:end is evaluated without Julia “knowing” that it represents indices of the previous argument. The “nice” notation can be used with the macro @view:

@view a[30:end]

(By the way, there is a mistake in your example: the function collection is not in Base; I think you meant collect.)


Also, it might be useful: A reference to X[end] in slicing operations is really a call to lastindex(X), so it makes sense this special keyword works only in the context of a slicing operation, rather than the call of view function.

Yes, you are correct. collection was wrong. Thanks for the tip!