[v; n]: a list or an array of n copies of a value that copies (ADR-362 D1-D4)Open and answer this issue on GitHub.
ready · opened 2026-10-17 · updated 2026-10-17 · 0 comments
The owner wrote let safety_buffer = [0..181] for 181 numbers (the loop program of #640). That is a list of one range; printing an element is NK1201 (#638), indexing it with 110 aborts. Asking how to write 181 equal elements without a loop showed a gap, checked with the compiler on 2026-10-17:
| Written | Today |
|---|---|
(0..<181).map(fn(_i) { 0 }).collect() |
Vec of 181 zeros |
let mut xs = [] then xs.resize(181, 0) |
Vec of 181 zeros (lowered to vec[0; 181], solver-workload §8.1) |
let xs: Array[i64, 181] = (0..<181).collect() |
NK1103 |
[0; 181] |
syntax error Expected ] here, but found ; |
An Array[T, N] of more than a handful of elements cannot be written at all, though ADR-152 made it the container without allocation.
[v; n] (Rust’s); rejected: a function repeat(v, n), [v] * n, [v ** n], nothing.NK1280), not cloned.Array[T, N]), Part III’s table (NK1157, NK1280).[expr; expr] as a list literal of its own kind (no other ; inside brackets). Test: [0; 3], [x + 1; n], nested [[0; 2]; 3]; [1, 2; 3] is a syntax error.Vec[T]; Array[T, N] wanted: n must be a comptime integer equal to N, else NK1157 naming both (also for an n the build does not know). For a Vec, n is any whole number type. Tests: let b: Array[u8, 4096] = [0; 4096] builds; [0; 4095] there is NK1157; let v = [0; n] with a parameter n is a Vec[i64].NK1280: v not a value that copies (String, Vec, a struct owning one); the help writes (0..<n).map(fn(_i) { v.clone() }).collect(). Test: [name; 3] with name: String refused with the help; [(1, 2); 3] and [Point { x: 0, y: 0 }; 3] (a value struct of numbers) build.vec[v; n as usize] for a Vec (a negative n aborts as an index out of range does, ADR-293: check before the cast), [v; N] for an array. Test: the output of a program printing len() and elements; a negative length aborts with the index message.len() of [v; n] is n (and N for an array). Test: let xs = [0; 181]; assert(xs.len() == 181) proved; an index b with 0 <= b && b < 181 known needs no check.tools/*.nika: replace the resize workarounds where one builds a list of equal values, if any (grep -n 'resize('); not required.The owner’s program then writes let safety_buffer = [0; 181].
Related: #640, #638; the warning for [0..181] (ADR-362 D5) is its own issue.