Arrays, slices and maps
| Type | What it is | Copies on assignment? |
|---|---|---|
T[N] |
A fixed-size array stored inline | Yes (a value type) |
Array<T> |
A growable array in an arena | No (a reference) |
Slice<T> |
A view of contiguous elements of an array | No (a view) |
Map<K, V> |
A hash table in an arena | No (a reference) |
Fixed arrays
struct Pair { items: int[2]; }
export function main(): void {
const a: int[3] = [1, 2, 3];
let b = a; // a full copy
b[1] += 10;
let pair = Pair([4, 5]);
pair.items[0]++;
let total = 0;
for (const v of a) total += v;
print(a[1], b[1], b.length, pair.items[0], total);
}
2 12 3 5 6
Nis a positive decimal integer. A literal must have exactlyNelements (fixed array literal has the wrong length).[1, 2, 3]without a type is anint[3]. Fixed arrays have nopush(int[3] has no method 'push').- Indexes must be integers. Out-of-bounds indexing stops the program (dev builds log the location first).
Growable arrays
export function main(): void {
const items = new Array<int>(); // frame arena
print(items.push(6, 7, 8), items.length); // push returns the new length
print(items.pop() ?? -1, items.length); // pop returns T | null
const kept = new(game) Array<string>(); // a longer-lived arena
kept.push("a", "b");
for (const s of kept) print(s);
}
3 3
8 2
a
b
new Array<T>()allocates fromframe(gone at the end of the frame);new(level),new(game),new(screen)andnew(heap)choose another arena.for...ofreads the current length on each step, so it sees elements pushed during the loop.
Slices
function sum(values: Slice<int>): int {
let total = 0;
for (const v of values) total += v;
return total;
}
export function main(): void {
const items = new Array<int>();
items.push(1, 2, 3, 4);
const tail = items.slice(2); // a view of [3, 4]
tail[0] = 30; // writes through to items
print(items[2], sum(tail), sum(items.slice(-3, -1)));
let fixed: int[3] = [5, 6, 7];
const view = fixed.slice(1); // a local fixed array can be sliced too
print(sum(view));
}
30 34 32
13
slice(start?, end?)clamps out-of-range ends; negative values count from the end.- A slice refers to the buffer that was current when it was made. If the array grows later, old slices keep pointing at the old buffer.
- To slice a fixed array, it must be a non-null local variable
(
fixed array slice requires a non-nullable local binding). Copy a field into a local first.
Callbacks: sort, filter, map, find, forEach
struct Row { key: int; id: int; }
export function main(): void {
const items = new Array<int>();
items.push(3, 1, 4, 2);
items.sort((a, b) => a - b); // stable, in place, returns items
const big = items.filter((n) => n >= 3); // a new frame array
const labels = items.map((n, i) => `${i}:${n}`);
const halves = items.map<float>((n) => n / 2.0);
print(items[0], items[3], big.length, labels[1], halves[3]);
print(items.find((n) => n > 2) ?? -1, items.find((n) => n > 9) ?? -1);
items.slice(0, 2).forEach((n, i) => print("item", i, n));
const rows = new Array<Row>();
rows.push(Row(2, 1), Row(1, 2), Row(2, 3));
rows.sort((a, b) => a.key - b.key);
print(rows[0].id, rows[1].id, rows[2].id); // stable: 1 stays before 3
}
1 4 2 1:2 2
3 -1
item 0 1
item 1 2
2 1 3
- The
sortcomparator returns anint: negative, zero or positive. - The other callbacks receive the element and its
intindex; you may leave off either. filterandfindneed aboolresult;map's result type comes from the callback or frommap<U>.- They work on
Array<T>andSlice<T>.
Maps
export function main(): void {
const scores = new(game) Map<string, int>();
scores.set("ana", 1).set("ben", 2); // set returns the map
scores.set("ana", 10); // updating keeps its position
print(scores.size, scores.has("ben"), scores.get("ana") ?? 0, scores.get("zoe") ?? -1);
print(scores.delete("ben"), scores.delete("ben"));
scores.set("cy", 3);
for (const entry of scores) print(entry.key, entry.value);
}
2 true 10 -1
true false
ana 10
cy 3
- Keys can be numbers, bools, strings, literal unions, class references and handles.
Strings compare by bytes, references by identity. Fixed arrays and structs are not
hashable (
Map key type int[1] is not hashable). getreturnsV | null.- Iteration is in insertion order and yields copies with
.keyand.value. new Map<K, V>()uses theframearena like arrays do.
Collections that outlive a frame
Allocate them in game, level or screen and store the reference somewhere that lives
as long: a class static field or a @transient component field. A heap collection
must be released with free(x). See Memory.