5.1.10. Tables

This tutorial covers declaring tables, inserting and erasing entries, safe lookup with ?[], block-based access with get(), iteration, table comprehensions, and key-only tables (sets).

5.1.10.1. Declaring a table

A table maps keys to values. Note the semicolon between key and value types:

var ages : table<string; int>

5.1.10.2. Inserting values

Use insert with pipe syntax:

ages |> insert("Alice", 30)
ages |> insert("Bob", 25)
ages |> insert("Carol", 35)

Inline construction uses { key => value } with commas:

var scores <- { "math" => 95, "english" => 87, "science" => 92 }

5.1.10.3. Safe lookup

?[] returns the value if the key exists, falling back to the value after ??. It does not insert missing keys:

let age = ages?["Alice"] ?? -1     // 30
let unknown = ages?["Dave"] ?? -1  // -1

Use key_exists to test for a key:

key_exists(ages, "Alice")    // true
key_exists(ages, "Dave")     // false

Note

The plain [] operator on a table inserts a default entry if the key is missing and requires unsafe. Prefer ?[] or get() for safe lookups.

5.1.10.4. Block-based lookup

get() runs a block only if the key is found:

get(ages, "Bob") $(val) {
    print("Bob is {val} years old\n")
}

Pass var in the block parameter to modify the value in-place:

get(scores, "math") $(var val) {
    val = 100
}

5.1.10.5. Erasing

ages |> erase("Bob")

5.1.10.6. Iteration

Use keys() and values() as parallel iterators:

for (subject, score in keys(scores), values(scores)) {
    total += score
}

Note

Tables are hash-based — iteration order is not guaranteed.

5.1.10.7. Table comprehensions

Build a table from a for expression:

var squareMap <- { for (x in 1..6); x => x * x }

5.1.10.8. Key-only tables (sets)

A table<KeyType> with no value type acts like a set:

var seen : table<string>
seen |> insert("apple")
seen |> insert("banana")
seen |> insert("apple")    // no effect — already present

key_exists(seen, "apple")  // true

5.1.10.9. Safety notes

  1. tab[key] inserts a default value when the key is missing. It requires unsafe. Use ?[] or get() instead.

  2. Never write tab[k1] = tab[k2] — the first [] may resize the table, invalidating the reference from the second [].

  3. Tables cannot be copied with = — use <- (move) or := (clone).

5.1.10.10. Running the tutorial

daslang.exe tutorials/language/10_tables.das

Full source: tutorials/language/10_tables.das

5.1.10.11. See also

Next tutorial: Tuples and Variants