Arrays
Arrays are ordered collections of elements of the same type. They use square brackets for the type and curly braces for values.
Declaring Arrays
Use the [type] syntax to declare an array:
temp numbers [int] = {1, 2, 3, 4, 5}
temp names [string] = {"Alice", "Bob", "Charlie"}
temp flags [bool] = {true, false, true}
Empty Arrays
Use {} to create an empty array:
temp empty [int] = {}
temp noNames [string] = {}
Accessing Elements
Arrays are zero-indexed. Use arr[index] to access elements:
temp numbers [int] = {1, 2, 3, 4, 5}
std.println(numbers[0]) // 1
std.println(numbers[2]) // 3
std.println(numbers[4]) // 5
Note: Accessing an index outside the valid range produces a runtime error.
Modifying Elements
Assign to an index to modify an element (only for temp arrays):
temp numbers [int] = {1, 2, 3}
numbers[0] = 100
std.println(numbers) // {100, 2, 3}
Array Length
Use len() to get the number of elements:
temp numbers [int] = {1, 2, 3, 4, 5}
temp names [string] = {"Alice", "Bob"}
temp empty [int] = {}
std.println(len(numbers)) // 5
std.println(len(names)) // 2
std.println(len(empty)) // 0
Fixed-Size Arrays
Fixed-size arrays have a maximum length specified at declaration and must use const:
const DAYS [string, 7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
const PRIMES [int, 5] = {2, 3, 5, 7, 11}
const MATRIX [int, 9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
You can provide fewer values than the declared size — a warning is issued but it’s not an error. This is useful when you want to hard-code additional values later:
const SLOTS [int, 5] = {0, 1, 2} // OK - warning, but valid (3 of 5 slots used)
However, providing more values than the declared size is an error:
const SLOTS [int, 5] = {0, 1, 2, 3, 4, 5} // Error! 6 values exceeds size of 5
Important: Fixed-size arrays must be declared with
constbecause their size is fixed at checktime.
Multi-Dimensional Arrays
Use nested bracket syntax for multi-dimensional arrays:
// 2D array (matrix)
temp matrix [[int]] = {{1, 2}, {3, 4}, {5, 6}}
// 3D array
temp cube [[[int]]] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
// Accessing elements
temp row [int] = matrix[0] // {1, 2}
temp value int = matrix[1][1] // 4
matrix[0][1] = 99 // modify element
Jagged Arrays
Inner arrays can have different lengths:
temp jagged [[int]] = {{1, 2, 3}, {4, 5}, {6}}
// jagged[0] has 3 elements
// jagged[1] has 2 elements
// jagged[2] has 1 element
Iterating Multi-Dimensional Arrays
temp matrix [[int]] = {{1, 2}, {3, 4}, {5, 6}}
// Iterate over rows
for_each row in matrix {
std.println(row)
}
// Iterate over all elements
for_each row in matrix {
for_each value in row {
std.println(value)
}
}
Note: Fixed-size multi-dimensional arrays are not currently supported. Use
tempfor all multi-dimensional array declarations.
Byte Arrays
Byte arrays are specialized arrays for binary data, buffers, or raw file contents:
// Dynamic byte array
temp buffer [byte] = {0, 128, 255}
temp empty [byte] = {}
// Fixed-size byte array (must use const)
const HEADER [byte, 4] = {137, 80, 78, 71} // PNG magic bytes
Note: Byte values can be written as decimal (0–255) or hexadecimal (0x00–0xFF). For most use cases, the dynamic
[byte]type is recommended over fixed-size byte arrays.
Iterating
for_each
Iterate over every element:
temp names [string] = {"Alice", "Bob", "Charlie"}
for_each name in names {
std.println(name)
}
for_each with Index
Use a comma-separated pair to get the index (starting at 0):
temp fruits [string] = {"apple", "banana", "cherry"}
for_each i, fruit in fruits {
std.println("${i}: ${fruit}")
}
// 0: apple
// 1: banana
// 2: cherry
for with range()
Use for with range() and len() for index-based iteration:
temp numbers [int] = {10, 20, 30, 40, 50}
for i in range(0, len(numbers)) {
std.println("numbers[${i}] = ${numbers[i]}")
}
Membership
Use in and not_in to check if a value exists in an array:
temp numbers [int] = {1, 2, 3, 4, 5}
if 3 in numbers {
std.println("Found 3!")
}
if 10 not_in numbers {
std.println("10 is not in the array")
}
Arrays as Function Parameters
Arrays can be passed to functions like any other type:
do printAll(items [string]) {
for_each item in items {
std.println(" - ${item}")
}
}
do main() {
temp names [string] = {"Alice", "Bob", "Charlie"}
printAll(names)
}
Mutable Parameters
By default, array parameters are read-only. Use & to allow modification:
do addDefaults(&items [int]) {
items[0] = 0
}
do main() {
temp numbers [int] = {1, 2, 3}
addDefaults(numbers)
std.println(numbers) // {0, 2, 3}
}
Note: Only
tempvariables can be passed to mutable (&) parameters. Passing aconstvariable to a¶meter will produce an error.
Const vs Temp Arrays
temp arrays can be modified; const arrays cannot:
// Mutable - elements can be changed
temp numbers [int] = {1, 2, 3}
numbers[0] = 100 // OK
// Immutable - elements cannot be changed
const FIXED [int] = {1, 2, 3}
// FIXED[0] = 100 // Error! Cannot modify const
Fixed-size arrays always require const:
const DAYS [string, 7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
Example Program
import @std
import @arrays
using std
do main() {
println("=== Arrays in EZ ===")
// Declaring arrays
temp numbers [int] = {1, 2, 3, 4, 5}
temp names [string] = {"Alice", "Bob", "Charlie"}
println("numbers: ${numbers}")
println("names: ${names}")
// Accessing elements (0-indexed)
println("numbers[0] = ${numbers[0]}")
println("names[1] = ${names[1]}")
// Modifying elements
numbers[0] = 100
println("After numbers[0] = 100: ${numbers}")
// Array length
println("Length of numbers: ${len(numbers)}")
println("Length of names: ${len(names)}")
// Iterating with for_each
println("All names:")
for_each name in names {
println(" - ${name}")
}
// Iterating with index
for i in range(0, len(numbers)) {
println(" numbers[${i}] = ${numbers[i]}")
}
// Empty array
temp empty [int] = {}
println("Empty array length: ${len(empty)}")
}
See Also
- Types — primitive and composite types
- @arrays — array manipulation functions (append, remove, sort, etc.)
- Functions — arrays as parameters and mutable
¶ms - Control Flow —
for_eachandforloops for iteration