I was looking for a way to access the last element of a sequence. In the documentation, I easily found a[a.high] but I wanted something shorter, like Python a[-1].
It took me a surprisingly long time to find a[^1]. I think that this feature deserves to be mentioned in the documentation, near the description of high and low. Currently it can be deduced from the description of slices but this is not at all obvious.
I see that this fails on arrays that are not indexed from 0:
var x: array[1..4, int] = [2, 4, 6, 8]
echo x[^1]
Error: invalid context for '^' as len!=high+1 for 'x'
I understand that x[^k] is a shortcut for x[x.len - k], and cannot be used when len!=high+1. In the latter case, would it be possible to make x[^k] a shortcut for x[x.high-k+1] ? This way, x[^1] would always return the last element whatever the indexing.