Capy-Bearer Documentation

array

Capy

[element_type]

An array holds a variable number of values of one type, written [s32], [string], and so on. Literals use square brackets and indexing is zero-based:

Use var values : [s64] = [] for an empty typed array. A bare var values := [] has no element type and fails compilation. any([]) creates a dynamic list instead. Convert explicitly with any(values) when a function such as join requires a dynamic list.

An index outside the current length traps. Assignment replaces an existing item and does not grow the array. Use push or resize before writing a new position.

function RENDER()
{
	var scores := [10, 20, 30]
	print(scores[0], " ", length(scores), "\n")
}

Output

10 3

Two names, one array

Assignment, parameter passing, returning and capturing all share one array — they do not copy it. A function that mutates an array's contents changes what the caller sees:

function append_total(values : [s32])
{
	values.push(60)
}

function RENDER()
{
	var values : [s32] = [10, 20]
	append_total(values)
	print(length(values), " ", values[2], "\n")
}

Output

3 60

The binding itself is immutable, so values = [3] inside that function is a compile error — you can change the contents, not which array the name refers to. Growth preserves identity, so a push that reallocates is still the same array to every holder.

Methods

push, pop, insert, remove, clear, reserve, resize and capacity are called on the array itself. length() returns the count.

Iteration

A for loop reads items in order, with an optional s64 index after the value:

function RENDER()
{
	var names := ["Ada", "Grace"]
	for name, index := names
	{
		print(index, ": ", name, "\n")
	}
}

Output

0: Ada
1: Grace

The loop rechecks the current length each iteration, so it stops safely after a shrink and can visit items appended while it runs. The loop variable is not an assignable position. Use an index to replace an item. A managed item still shares its object, so changing a field through the loop variable changes the original item.

var scores := [10, 20, 30]
scores.push(40)
print(scores[0], " ", length(scores), "\n")

Output

10 4