All examples
Language

Nested collections and shared values

Combine Lists, Dictionaries, and user objects, inspect shallow copies, and traverse Dictionary snapshots in insertion order.

Download .aner source
collections_nested.aner
// Any payloads retain their concrete type, including nested references.
class Record {
    public var label: String
}
let record = Record(label: "first")
let child = List(10, "hello")
let lookup = Dictionary()
lookup.set("record", record)
lookup.set("values", child)
let entries = List(lookup, child)

// copy() separates the outer container and shares nested references.
let copied = entries.copy()
cast<List>(copied.get(1)).append(true)
print(child.len())
cast<Record>(cast<Dictionary>(entries.get(0)).get("record")).label = "updated"
print(record.label)
print(entries == copied)

// Dictionary snapshots are Lists in deterministic insertion order.
let keys = lookup.keys()
var index = 0
while index < keys.len() {
    let key = keys.get(index)
    print(key)
    print(type_of(lookup.get(key)))
    index = index + 1
}

Make it your experiment.

With Aner installed, save this program as examples/collections_nested.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/collections_nested.aner
aner run examples/collections_nested.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner