Skip to content

Arrays

Arrays are Munos's structured-data primitive. Every array has a fixed element type known at compile time, written T[].

Declaration

Two forms:

munos
var scores: i32[] = [10, 20, 30]   // literal: size and values from the initializer
var slots: i32[10]                 // sized: ten elements, zero-filled

A sized declaration is itself an initializer — its elements are zero-filled (numbers → 0, boolfalse, string""). The "must-initialize" rule applies to arrays; the size is the initialization.

Indexing

Zero-based, square brackets. Out-of-bounds access traps at runtime.

munos
var scores: i32[] = [10, 20, 30]
var first = scores[0]   // 10
scores[1] = 99

Length and capacity

An array header tracks two numbers:

  • length — how many elements are currently in use. len(x).
  • size (capacity) — how many it can hold. size(x).
munos
len(scores)    // current number of elements (also works on strings → byte length)
size(scores)   // capacity

For both literal and sized declarations the array starts fulllen == size. They differ only after you pop (see below).

Growable operations

push and pop change the length within the fixed capacity. Capacity never grows.

munos
push(arr, value)   // append at index len, bump length; TRAPS if len == size
pop(arr)           // remove and return the last element; TRAPS if len == 0

Because a freshly declared array is already full, the working pattern is pop to make room, then push:

munos
var a: i32[3]
a[0] = 10; a[1] = 20; a[2] = 30
pop(a)        // 30 — len now 2
pop(a)        // 20 — len now 1
push(a, 99)   // a[1] = 99, len now 2
push(a, 7)    // len now 3
// push(a, 1) // would trap: len == size == 3

push's value must match the element type; pop returns the element type.

copy

copy(x) returns an independent duplicate of an array or string — a fresh backing buffer with size == len == src.length.

munos
var a: i32[] = [1, 2, 3]
var b = copy(a)
b[0] = 99      // a[0] is still 1

copy accepts arrays and strings only — not structs (duplicate a struct by copying its fields).

Parallel arrays: the record idiom

Munos has no array-of-structs-by-value collection type. For a set of records — every player's x, y, and name — the idiom is parallel arrays indexed by the same key:

munos
var playerX: u8[64]
var playerY: u8[64]
var occupied: bool[64]

// player i's state is (playerX[i], playerY[i]), valid when occupied[i]

This is the backbone of server-side per-player state — see Server & slots. For a single record, use a struct.

Summary of array forms

FormResult typeNotes
len(a)i32current length; also strings
size(a)i32capacity
push(a, v)append; traps at capacity
pop(a)elementremove last; traps when empty
copy(a)same as aindependent duplicate (arrays/strings)

Part of the MultiNostalgia project.