Arrays - grivia0/projecttera GitHub Wiki
Arrays & Type Inference
Arrays in Tera follow the same directional assignment pattern, supporting typed collections and automatic type inference upon indexing.
1. Declaration & Typed Arrays
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];
2. Indexing & Type Inference
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;
3. Array Properties & Inspection
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.
4. Array Methods
Tera inherits standard JavaScript-style array methods for manipulation and inspection:
1. .asString()
Converts the array to a string representation.
let arr: [1, 2, 3] => arr[int]
let s: arr.asString()
print(s) // Output: 1,2,3
2. .at(index)
Returns 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: 200
3. .join(separator)
Joins 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-c
4. .rmv(index)
Removes 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: 20
5. .add(...args)
Appends 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,4
6. .shift() / .rmShift()
Removes the first element from an array and returns it.
let array: [10, 20, 30] => arr[int]
let first: array.shift()
print(first) // Output: 10
7. .merge(otherArray)
Concatenates 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,4
8. .copyIn(otherArray)
Creates 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,4
9. .flat()
Flattens nested arrays.
let array: [1, 2], [3, 4](/grivia0/projecttera/wiki/1,-2],-[3,-4) => arr[arr[int]] // (or untyped/inferred)
let flattened: array.flat()
print(flattened.asString()) // Output: 1,2,3,4
10. .slice(start, end)
Extracts 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,30
11. .splice(start, deleteCount, ...items)
Changes 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,5
12. Safe Access & Search Methods
Tera 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.
Code Examples
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;
});