- Introduction
- Editor
- Get started
- Runtime / VM
Language Guide
Design-Notes
Arrays
An Array is a dynamically sized, ordered collection of elements. Backed by contiguous native memory, Autolang arrays are heavily optimized for CPU cache locality.
Creating Arrays
Since Autolang is statically typed, you need to specify the type of elements the array will hold using Generics (<T>). Use the add() method to append items to the end of the array.
val numbers = Array<Int>()
numbers.add(10)
numbers.add(20)
numbers.add(30)
println("Size is: ${numbers.size()}")Accessing Elements
Autolang natively supports the index operator []. Under the hood, this compiles call get() and set() calls.
val priority = Array<String>()
priority.add("Sleep")
priority.add("Code")
priority.add("Eat")
// What matters most to a developer?
println(priority[0])
// Update an element
priority[2] = "Drink Coffee"- Arrays in Autolang are zero-indexed.
- Attempting to access an index out of bounds will result in a runtime exception.
Iteration
The cleanest and most efficient way to loop through an array is using the for...in syntax.
val tasks = Array<String>()
tasks.add("Fix bugs")
tasks.add("Create new bugs")
for (task in tasks) {
println(task)
}Methods Reference
add(value: T)- Appends an element to the end of the array.remove(index: Int)- Removes the element at the specified index, shifting subsequent elements.get(index: Int): T- Returns the element at the index. Equivalent toarr[index].set(index: Int, value: T)- Updates the element at the index. Equivalent toarr[index] = value.size(): Int- Returns the total number of elements currently in the array.clear()- Instantly removes all elements, resetting the size to 0.
