Docs/Language

Language

Arrays

GitHub

Array<T> construction, len/get/set, push/pop, and for-in iteration.

Builtin Array<T> is the primary growable sequence type in the MVP (RFC-001). Element types include Int, Bool, String, class references, interfaces, structs, enums (by value), and nested Array<Array<T>>.

Interface elements (C20h)

Array<I> is supported for interface I. Interface elements use the runtime's tagged representation, so get, set, copying, method dispatch, and GC marking preserve the concrete object behind each element. Immutable Arrays captured by lambdas are cloned as owning snapshots and register their backing buffers with the typed GC root contract, which scans each tagged element. See corpus/iface/array_interface.aura for a dispatch and GC smoke test. The same typed registration is used for nested aggregate arrays and compiler-generated async/spawn frame storage, so tagged elements are never scanned as raw pointers.

Create and index

aura
fun demo() {
  val xs: Array<Int> = Array(0)   // capacity hint; grow with push
  xs.push(10)
  xs.push(20)
  val n = xs.len                  // field: element count
  val first = xs.get(0)
  xs.set(0, 11)
  if (xs.isEmpty()) {
    // …
  }
}

Out-of-bounds access is a runtime failure — prefer checking len before get/set when input is untrusted.

Grow and shrink

Method / fieldBehavior
lenElement count (field)
isEmpty()len == 0
push(x)Append; capacity grows as needed
pop()Remove last; empty array throws
clone()Owning buffer copy (C9c); nested Arrays deep-copied
clear()len = 0, keep capacity
reserve(n)Ensure capacity ≥ n
aura
fun stack() {
  val xs: Array<Int> = Array(0)
  xs.push(1)
  xs.push(2)
  val top = xs.pop()
  xs.clear()
}

Higher-order helpers (C10i / C12o)

std.collections provides concrete and generic free functions that take first-class funs (see syntax cheatsheet for lambda forms). Generic array HOFs are available for the covered element families; ownership still follows the move rules.

HelperSignature
map_ints(Array<Int>, (Int) -> Int) -> Array<Int>
filter_ints(Array<Int>, (Int) -> Bool) -> Array<Int>
fold_ints(Array<Int>, Int, (Int, Int) -> Int) -> Int
map_strings(Array<String>, (String) -> String) -> Array<String> (C12o)
filter_strings(Array<String>, (String) -> Bool) -> Array<String> (C12o)
fold_strings(Array<String>, String, (String, String) -> String) -> String (C12o)
join(Array<String>, String) -> String (C12j)
aura
import std.collections

fun demo(xs: Array<Int>): Int {
  val doubled = map_ints(xs, (x: Int) => x * 2)
  // Array params own the buffer — clone when reusing the same Array.
  return fold_ints(doubled.clone(), 0, (a: Int, b: Int) => a + b)
}

fun demo_str(xs: Array<String>): Array<String> {
  return map_strings(xs, (s: String) => "[" + s + "]")
}

Corpus: fun/lambda_hof.aura, std_collections/hof, std_collections/hof_str, std_collections/join.

Capture limits: lambdas may close over outer val of Int / Bool / String / class / Array and outer var of scalar, String, class, Array, or Fun values through shared mutable storage (C20c–e). Mutable class payloads are GC-rooted and nested Fun environments retain/release correctly. Array captures use an owned snapshot for immutable bindings and a shared retained cell for mutable bindings; they do not expose non-owning live views, so owner rebinding, closure escape, mutation, and forced GC remain valid under the documented retain/release contract. Array<HeapClass> and Array<Interface> snapshots register an explicit typed GC array root for the closure lifetime. Richer aggregate element policies remain tracked in agents/debts.md.

Array parameters own the buffer (move at call site). Use clone() if you need the same array after a call that takes it.

Iteration

aura
fun sum(xs: Array<Int>): Int {
  var total = 0
  for (x in xs) {
    total = total + x
  }
  return total
}

fun indices(xs: Array<Int>) {
  for (i in 0..xs.len) {
    println(xs.get(i))
  }
}

String iteration over UTF-8 bytes as Int is also supported (for (b in string)). Duck / interface Iterable (len + get) works via std.collections.Iterable<E> and matching implementors.

Arrays of class references

Array can hold class instances (heap references). Equality of class elements remains identity unless you compare fields explicitly.

Ownership notes (implementation)

The C backend frees owned array buffers at scope end / before return / on owner reassignment. Prefer clear lifetime patterns in local scopes (RFC-003, RFC-006).

Element drop:

  • Nested Array<Array<T>>: deep-frees nested buffers on drop / clear / set (C8e / C8f).
  • Other elems: buffer-only free is enough for primitives; structs, enums, and interface unions use generated typed clone/drop/mark hooks, while heap class references are scanned as GC roots.

Corpus

See corpus/generic/array*.aura and related control samples for executable truth.

Next

All docs pages