Types and collections

Learning objective

  • Apply Lucia types and collection structures in real code.

Key syntax

let age: int = 20;
let price: float = 12.5;
let name: string = "Ana";
let active: bool = true;
let tags: list<string> = ["a", "b"];
let user: dict<string, any> = {"name": "Ana", "age": 20};

Examples

  • Access list values: tags[0].
  • Access dictionary values: user["name"].
  • Use date(2026, 3, 15) and datetime(...) for temporal values.

Practical patterns

  • Empty list and dict literals with explicit declaration type:
let mazo: list<Carta> = [];
let puntajes: dict<string, int> = {};
  • Reassigning empty collections keeps the variable type:
mazo = [];
puntajes = {};
  • Indexed reads and writes:
let temp: Carta = mazo[i];
mazo[i] = mazo[j];
puntajes["ana"] = 10;
  • Type-safe list and dict methods:
mazo.append(Carta("A"));
if (puntajes.contains_key("ana")) {
	print(puntajes.get("ana", 0));
}

Notes

  • Use len(x) to get collection size.
  • list<T> requires compatible elements on assignment and append.
  • dict<K, V> validates both key and value types.
  • Common list methods: insert, remove, removeAt, extend, first, last, is_empty, copy, clear.
  • Common dict methods: items, size, merge, put, remove, is_empty, clear.

Common mistakes

  • Assigning incompatible values to typed variables.
  • Using invalid index/key expressions.
  • Trying to call methods that are not supported on the receiver type (for example length() on list).

Suggested practice

  • Model a student record with dict<string, any> and a list of grades.

Extended practice

  • Build a deck shuffler:
  1. Define list<Carta>.
  2. Use a for loop with random(0, n - 1).
  3. Swap elements with indexed assignment.
  • functions-and-builtins
  • methods-and-chaining