Skip to content
kata / a language workbench

Collections

Arr[T] and Map[K, V] are available through the prelude. Their implementations live in KataScript’s standard library.

Array literals infer an element type from their values. Elements must have a compatible type.

let values = [10, 20, 30]
values.push(40)
values[1] = 25
print(values.len) # 4
print(values[1]) # 25
print(values.get(99).unwrap_or(0)) # 0

An empty literal has no element type to infer. Create an empty array explicitly:

let values = Arr[Int].new()
values.push(7)
print(values.pop().unwrap_or(0))
OperationResult
array.lenNumber of elements; a field
array.push(value)Append one element
array.pop()Opt[T] containing the last element, or Non
array.get(index)Opt[T]; absent when out of bounds
array.set(index, value)Bool indicating whether the index existed
array[index]Element; out-of-bounds access stops execution
array[index] = valueReplace an existing element
let scores = Map[Str, Int].new()
scores["Ada"] = 42
scores["Lin"] = 37
print(scores.len()) # 2
print(scores["Ada"]) # 42
print(scores.get("Sam").unwrap_or(0))

Use get when a key may be absent. Indexing assumes it exists and stops execution otherwise. set(key, value) inserts or replaces a value, has(key) tests presence, and del(key) returns whether it removed a key.

Repeated insert/delete cycles have a known corruption bug. Floating-point keys also have equality/hash defects. These examples use string keys and avoid deletion.

let scores = Map[Str, Int].new()
scores["Ada"] = 42
scores["Lin"] = 37
let total = 0
for (_, score) in scores {
total = total + score
}
print(total)

Map iteration yields (key, value) tuples. Do not depend on its order; look up a known key sequence when output order matters.

The current runtime does not give collection aliases coherent independent storage. After let other = values, growing other can invalidate values. Passing or returning a collection also deserves care; these operations are not deep-copy guarantees.

Keep growth on one direct binding, avoid mutation during iteration, and do not rely on generic Drop for cleanup. These are current implementation limits, not a completed ownership model.