Lists & Maps
Store multiple values and look them up by key.
List<T> stores elements of one type; Map<K,V> associates keys with values. Here we count labels and write the result into a map.
Complete example
Save the example as collections.aal
agent Tutorial is Agent
public
ability run() -> Int effects()
labels = ["docs", "tools"]
labels.append("agents")
counts = Map<String,Int>{"total": 0}
counts["total"] = labels.len()
return counts["total"]Syntax explained
-
Write a list as ["docs", "tools"]. Access with
items[0]and add with.append(value). -
An explicit map is
Map<String,Int>{"total": 0}; read or write usingmap["total"]. -
Lists and records have value semantics: assignment and calls clone owned values. Local collection mutation does not
requirememory.write.
Build and run
cargo run --locked --bin aalc -- check collections.aal
cargo run --locked --bin aalc -- build collections.aal -o collections.aalc
cargo run --locked --bin aalc -- run collections.aalc \
--agent Tutorial --ability run \
--args '{}' \
--state-dir .aal-tutorial/collections --invocation collections-1Run with a fresh state directory. The returned value is shown below; the CLI also prints execution information.
3Try it yourself
Add labels.append("notes"), rebuild and use a new invocation ID. The returned count should change from 3 to 4.