Repository navigation
Arrays
Arrays in Tera follow the same directional assignment pattern, supporting typed collections and automatic type inference upon indexing.
You can declare collections with explicit type definitions using the arr[type] syntax:
// Explicitly typed array holding integers
let nums: [1, 2, 3] => arr[int];
// Array of strings
let names: ["Tera", "Cat"] => arr[string];Because an array like nums is explicitly typed as arr[int], Tera automatically infers the extracted value type when you access an element, meaning you don't need to write an explicit static annotation unless you want to:
// Type 'int' is inferred automatically from nums
let first: nums[0];
// Can still be written explicitly if desired:
let second: nums[1] => int;Tera provides convenient built-in properties for inspecting arrays and collections:
-
x.leng: Returns the length of the array (equivalent to JS.length). -
x.first: Retrieves the first element of the array. -
x.last: Retrieves the last element of the array. -
x.isEmpty: Returns a boolean check (trueorfalse) indicating whether the array has no elements.
Tera inherits standard JavaScript-style array methods for manipulation and inspection:
Converts the array to a string representation.
let arr: [1, 2, 3] => arr[int]
let s: arr.asString()
print(s) // Output: 1,2,3Returns the element at the specified index (supports negative indexing like standard JavaScript at).
let array: [100, 200, 300] => arr[int]
let val: array.at(1)
print(val) // Output: 200Joins all elements of an array into a string using the provided separator (defaults to ,).
let array: ["a", "b", "c"] => array[string]
let j: arr.join("-")
print(j) // Output: a-b-cRemoves and returns the element at the specific index from the array in-place.
let array: [10, 20, 30] => arr[int]
let removed: array.rmv(1)
print(removed) // Output: 20Appends new elements to the end of the array.
let array: [1, 2] => arr[int]
array.add(3, 4)
print(array.asString()) // Output: 1,2,3,4Removes the first element from an array and returns it.
let array: [10, 20, 30] => arr[int]
let first: array.shift()
print(first) // Output: 10Concatenates another array to the current one and returns the new combined array.
let a1: [1, 2] => arr[int]
let a2: [3, 4] => arr[int]
let combined: a1.merge(a2)
print(combined.asString()) // Output: 1,2,3,4Creates a shallow copy combining elements using the spread syntax behavior ([...obj, ...other]).
let a1: [1, 2] => arr[int]
let a2: [3, 4] => arr[int]
let copied: a1.copyIn(a2)
print(copied.asString()) // Output: 1,2,3,4Flattens nested arrays.
let array: [[1, 2], [3, 4]] => arr[arr[int]] // (or untyped/inferred)
let flattened: array.flat()
print(flattened.asString()) // Output: 1,2,3,4Extracts a section of an array and returns a new array.
let array: [10, 20, 30, 40] => arr[int]
let sub: array.slice(1, 3)
print(sub.asString()) // Output: 20,30Changes the contents of an array by removing or replacing existing elements and/or adding new elements in-place.
let array: [1, 2, 5] => arr[int]
array.splice(2, 0, 3, 4) // Insert 3 and 4 at index 2
print(array.asString()) // Output: 1,2,3,4,5Tera provides advanced methods for safe element retrieval and searching using predicates:
-
x.get(i): Retrieves the element at indexi. Unlike standard indexing or.at(), this method explicitly throws a runtime error if the index is out of bounds. -
x.find(pred): Evaluates each element against a predicate function and returns the first matching element. -
x.findIndex(pred): Evaluates each element against a predicate function and returns the index of the first matching element.
let nums: [10, 20, 30, 40] => arr[int];
// 1. Safe bounds-checked retrieval
let val: nums.get(1); // Returns 20
// nums.get(10); // Throws an out-of-bounds error!
// 2. Finding elements and indices using lambda predicates
let found: nums.find(fn(n: int) => bool {
return n > 25;
});
let index: nums.findIndex(fn(n: int) => bool {
return n > 25;
});