LYR-SEM0114
a from-the-end index '^n' outside the place it means something
'^n' stands outside '[…]' as a value of its own, indexes a string directly, or indexes a value that has no length.
Explanation
^n counts from the end of what is being indexed, so it is sugar inside the brackets only: xs[^1], xs[1..^1]. On its own (let i = ^1;) it has no length to count from. A string's index is a byte offset where a character begins, which from the end is rarely the case, so on a string ^n bounds a range only (s[^3..]). And a value without a length has no end to count from. A type with its own index counts from its end where it conforms to Length — list[^1] on a List<T> is list.index(list.length() - 1), list evaluated once —; one without Length takes the index as written, so ^n is refused there.
Write ^n inside the index, compute the position as xs.length() - n, give the type Length, or slice a string with a range.
Wrong
fn last(xs: int[]): int {
let i = ^1;
return xs[i];
}
fn main(): void {
let _ = last([1, 2, 3]);
}
Right
fn last(xs: int[]): int {
return xs[^1];
}
fn main(): void {
let _ = last([1, 2, 3]);
}