Collections & text
Arrays
Creating Arrays
# With initialization
array[int] numbers = new array[int]{1, 2, 3, 4, 5}
# With a size (allocates 10 cells, each holding the element type's default)
array[int] sized = new array[int](10) # ten zeroes
# Multidimensional arrays
array[array[int]] matrix = new array[array[int]]{
new array[int]{1, 2, 3},
new array[int]{4, 5, 6}
}Default Contents
A sized array starts every cell at its element type's default — the same value a bare T x declaration gives a variable:
| Element type | Starting value |
|---|---|
byte, int, long, float | 0 |
bool | false |
str | "" |
ptr[T] | null |
So a counting loop needs no setup pass:
array[int] counts = new array[int](26)
int i = 0
while (i < word.len()) {
counts[word[i] - 97] += 1 # every cell starts at 0
i = i + 1
}The remaining element types — classes, enums, nested arrays and functions — have no default, so their cells start unassigned. Reading one before assigning it raises a catchable ERR_BOUNDS error rather than yielding a placeholder value:
array[array[int]] grid = new array[array[int]](3)
try {
int first = grid[0][0] # ERR_BOUNDS — row 0 was never assigned
} catch (Error e) {
term.println(e.kind) # bounds
}
grid[0] = new array[int](8) # assign the row first
term.println(grid[0][0].str()) # 0Writing such a cell is always fine; only reading an unassigned one is an error. See Error handling.
Accessing Elements
array[int] arr = new array[int]{10, 20, 30}
int first = arr[0] # 10
int second = arr[1] # 20
arr[2] = 40 # Change elementArray Properties and Methods
len — Array Length
array[int] arr = new array[int]{1, 2, 3}
int length = arr.len() # 3sub(start, end) — Subarray
array[int] arr = new array[int]{1, 2, 3, 4, 5}
array[int] sub = arr.sub(1, 4) # [2, 3, 4]con(other) — Concatenation
array[int] a = new array[int]{1, 2}
array[int] b = new array[int]{3, 4}
array[int] combined = a.con(b) # [1, 2, 3, 4]plus(element) — Add Element
array[int] arr = new array[int]{1, 2}
array[int] extended = arr.plus(3) # [1, 2, 3]map(func) — Transform Elements
The callback takes the value first; the index is an optional second parameter.
array[int] numbers = new array[int]{1, 2, 3, 4, 5}
array[int] doubled = numbers.map(
func(int value) int {
return value * 2
}
) # [2, 4, 6, 8, 10]
# With the index:
array[int] offset = numbers.map(
func(int value, int index) int {
return value + index
}
) # [1, 3, 5, 7, 9]