Module collections/array_list
Dynamic array (vector) with amortized O(1) push and O(1) indexed access.
Types
Generic growable array similar to Rust's Vec<T>.
Heap-allocated with automatic resizing and reference-counted cleanup.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Trait Implementations
impl(forall(T : Type), ArrayList(T), ...)
len : (fn(self : Self) -> usize)Returns: usize
capacity : (fn(self : Self) -> usize)Returns: usize
ptr : (fn(self : Self) -> ?*(T))Get a pointer to the underlying data buffer Returns .None if the list is empty or has no allocated buffer
Returns: ?*(T)
as_slice : (fn(self : Self) -> Option(Slice(T)))Get a slice view of the ArrayList Returns a fat pointer Slice(T) with data pointer and length This allows passing ArrayList to functions expecting slices Returns .None for an empty ArrayList with no allocated buffer
Returns: Option(Slice(T))
set_len : (fn(self : Self, new_len : usize) -> unit)Directly set the length of the ArrayList UNSAFE: This does not initialize new elements or free removed elements Only use when you've manually populated the buffer (e.g., via direct memory writes)
Returns: unit
new : (fn() -> Self)Create a new empty ArrayList
Returns: Self
with_capacity : (fn(cap : usize) -> Self)Create an ArrayList with a specific initial cap
Returns: Self
push : (fn(self : Self, value : T) -> Result(unit, ArrayListError))Add an element to the end of the ArrayList
Returns: Result(unit, ArrayListError)
pop : (fn(self : Self) -> Option(T))Remove and return the last element
Returns: Option(T)
get : (fn(self : Self, index : usize) -> Option(T))Get an element by index (bounds checked)
Returns: Option(T)
shrink_to_fit : (fn(self : Self) -> Result(unit, ArrayListError))Shrink the capacity to match the current length This reduces memory usage but may cause reallocation on next push
Returns: Result(unit, ArrayListError)
_free_elements : (fn(self : Self) -> unit)Returns: unit
remove : (fn(self : Self, start : usize, (count : usize) ?= 1) -> Result(usize, ArrayListError))Remove elements from the ArrayList starting at index, removing count elements Returns the number of elements actually removed
Returns: Result(usize, ArrayListError)
slice : (fn(self : Self, start : usize, end : usize) -> Result(Self, ArrayListError))Create a new ArrayList containing a slice of elements from start to end (exclusive) Similar to array slicing in many languages: array[start:end] Returns a new ArrayList with copied elements
Returns: Result(Self, ArrayListError)
ensure_total_capacity : (fn(self : Self, min_cap : usize) -> unit)Ensure the ArrayList can hold at least min_cap total elements
without further reallocation.
Returns: unit
extend_from_ptr : (fn(self : Self, src : *(T), count : usize) -> unit)Append count elements from a raw pointer using memcpy.
The caller must ensure src points to at least count valid elements.
Returns: unit
clear : (fn(self : Self) -> unit)Clear all elements but keep capacity
Returns: unit
fill_with_byte : (fn(self : Self, byte_val : int) -> unit)Fill all elements with a byte pattern using memset. Useful for zeroing bool/integer arrays in O(1). Only safe for types without RC (e.g., bool, u8, usize).
Returns: unit
resize_with_byte : (fn(self : Self, new_len : usize, byte_val : int) -> unit)Resize ArrayList to exactly new_len elements, filling new slots with
a byte pattern via memset. Does not call destructors on removed elements.
Only safe for trivial types (bool, u8, usize, etc.).
Returns: unit
impl(forall(T : Type), ArrayList(T), Index(usize)(...))
Output : TThe output type is the element type T.
index : (fn(ref(self) : Self, idx : usize) -> *(Self.Output))Returns a pointer to the element at the given index. Panics if the index is out of bounds.
Parameters
| Name | Type | Notes |
|---|---|---|
idx | usize |
Returns: *(Self.Output)
impl(forall(T : Type), ArrayList(T), Indexable(usize)(...))
Element : Tproject : (fn(ref(self) : Self, pos : usize) -> ref(T))Parameters
| Name | Type | Notes |
|---|---|---|
pos | usize |
Returns: ref(T)
impl(forall(T : Type), ArrayList(T), Dispose(...))
dispose : (fn(self : Self) -> unit)RAII destructor - automatically called when ArrayList goes out of scope
For object, this is called when the reference count drops to zero
Returns: unit
impl(forall(T : Type), ArrayList(T), ...)
into_iter : (fn(self : Self) -> ArrayListIter(T))Returns: ArrayListIter(T)
impl(forall(T : Type), ArrayList(T), ...)
iter : (fn(ref(self) : Self) -> _ArrayListPosIter)Returns: _ArrayListPosIter
impl(forall(T : Type), where(T <: Eq(T)), ArrayList(T), ...)
contains : (fn(self : Self, value : T) -> bool)Check if the list contains a value
Returns: bool
index_of : (fn(self : Self, value : T) -> Option(usize))Find the first index of a matching value Returns .Some(index) if found, .None otherwise
Returns: Option(usize)
impl(forall(T : Type), ArrayList(T), ...)
reverse : (fn(self : Self) -> unit)Reverse elements in place
Returns: unit
impl(forall(T : Type), where(T <: Ord(T)), ArrayList(T), ...)
sort : (fn(self : Self) -> unit)Sort elements in ascending order using insertion sort
Returns: unit
impl(forall(T : Type), where(T <: Eq(T)), ArrayList(T), Eq(ArrayList(T))(...))
impl(forall(T : Type), where(T <: Clone), ArrayList(T), Clone(...))
clone : (fn(ref(self) : Self) -> Self)Returns: Self
Error variants for ArrayList operations.
Variants
| Variant | Fields | Description |
|---|---|---|
AllocError | error: AllocError | Memory allocation failed. |
IndexOutOfBounds | index: usize, length: usize | Index is out of bounds for the current length. |
EmptyList | Attempted to access an element from an empty list. |
Value iterator for ArrayList - yields elements by value (T) Used by into_iter() / IntoIterator trait
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Trait Implementations
impl(forall(T : Type), ArrayListIter(T), Iterator(...))
Item : Tnext : (fn(ref(self) : Self) -> Option(T))Returns: Option(T)
Functions
array_list macro — construct an ArrayList(T) literal.
The element type T is inferred from the first element via typeof, so
at least one element is required.
Example
xs := array_list(i32(1), i32(2), i32(3));
names := array_list(`alice`, `bob`);
Expands to:
{
__first := <first elem>;
__tmp := ArrayList(typeof(__first)).new();
__tmp.push(__first);
__tmp.push(<elem 2>);
...
__tmp
}
Returns: Expr