A generic function can take an array’s length as a parameter, written N: size:
Example
fn<T, N: size> fill(value: T): T[N] { mut r: T[N]; for i in 0..N { r[i] = value; } return r;}mut zeros: int[8] = fill(0); // N is 8, from the type the result goes intoN is fixed by each call, so it has a few rules, and E150 is breaking one of them:
Example
struct Buffer<N: size> { used: int } // only a free function can have onefn<N: size> S.first(a: int[N]): int { ... } // not a method or an operatorfn<N: count> first(a: int[N]): int { ... } // the only kind after ':' is sizefn<N: size> first(a: N, b: int[N]): int { ... } // N is a length, not a typefn<N: size> first(N: int, a: int[N]): int { ... } // a parameter can't share its namefn<N: size> count(a: int[N]): int { N = 3; ... } // N can't be changedInside the function, N reads as an int, as in for i in 0..N. To change the number,
copy it first: mut n: int = N;. For a struct that holds several values, give it a
fixed-size array field (items: int[8]) or a List.
Run turf --explain E150 in a terminal to read this again.