Fixed Array
A fixed array is a contiguous sequence of elements whose size is known at compile time. The type of a fixed array with N elements of type T is written [N] T. The storage for all elements is part of the variable itself, so a fixed array declared as a local variable lives on the stack, and a fixed array inside a struct is stored inline within that struct.
values: [4] int; // Four ints, all initialized to 0.
values[0] = 10;
values[1] = 20;
values[2] = 30;
values[3] = 40;
Literal Initialization
values: [4] int = .[10, 20, 30, 40];
Subscripting
Fixed arrays support subscript access with bounds checking. The index must be in the range 0 to N - 1. Accessing an index outside this range produces a runtime error.
x := values[2]; // Read the third element.
values[2] = 99; // Write to the third element.
Count and Data
A fixed array has a .count field that returns the number of elements (which is always the compile-time constant N) and a .data field that returns a pointer to the first element.
values: [4] int = .[10, 20, 30, 40];
print("count is %\n", values.count); // 4
print("data is %\n", values.data); // pointer to values[0]
Iteration
Fixed arrays can be iterated with a for loop:
for values {
print("index [%] has value %\n", it_index, it);
}
Conversion to Array View
A fixed array can be used anywhere an array view is expected. The resulting view has its data pointing at the fixed array's storage and its count set to N.