LYR-SEM0115
a view of an inline array that lies in a frame
A view ('Slice
Explanation
An inline array T[n] is stored where it stands: in the frame for a local or a parameter. A view points into its storage, and may outlive the frame — returned, stored in an object, captured — after which it would point into freed memory. So a view of an inline array is taken only where the array lies in the heap: a field of an object, an element of an array.
Copy the array into a heap array (a.toArray()), declare the local as T[], or pass the inline array itself.
Wrong
fn total(xs: Slice<int>): int { return xs.length(); }
fn main(): void {
let a: int[3] = [1, 2, 3];
let _ = total(a[1..]);
}
Right
fn total(xs: Slice<int>): int { return xs.length(); }
fn main(): void {
let a: int[] = [1, 2, 3];
let _ = total(a[1..]);
}