Skip to content

Type Contracts

Nilakan type hints are persistent contracts. Known shapes are checked statically, and unknown shapes are checked recursively at runtime when assignments, parameters, and returns are evaluated.

x: num = 1
name: str = "nilakan"
ok: bool = True
xs: list[num] = [1, 2, 3]
pair: tuple[str, num] = ("a", 1)
lookup: dict[str, num] = {"a": 1}
value: num | str = "unknown"
fallible: num | err = [1].get(10)
Run in playground

Supported scalar type names are num, str, bool, and err.

Supported container type names are list, tuple, set, and dict.

Container contracts can be bare or parameterized:

  • list[T]
  • set[T]
  • dict[K, V]
  • tuple[T1, T2, ...]

Tuple contracts with arguments are fixed-size positional contracts. Unions use the pipe operator.

Contracts are checked for:

  • top-level assignments
  • function arguments
  • function return values
  • init state assignments
  • whole primed updates
  • sparse updates into typed containers
def bump(xs: list[num]) -> list[num]:
init:
values: list[num] = xs
done: bool = False
while not done:
values'[0] = values[0] + 1
done' = True
return values
Run in playground

Unknown type names are rejected. Scalar types do not accept type arguments. list and set accept zero or one type argument. dict accepts zero or two type arguments. A union must contain at least two distinct options.

Duplicate union options are deduplicated. A union that collapses to one distinct option is invalid.

Deep container contracts are checked recursively.

table: dict[str, list[tuple[num, bool]]] = {
"rows": [(1, True), (2, False)]
}
Run in playground

Direct-error permission is intentionally shallow. Exact root err requires a direct error, and a root union containing err accepts one. Other roots—including no annotation—reject direct errors. A nested err permits errors only in that annotated container position.

def first(xs: list[num]) -> num | err:
return try xs.get(0)
bad: err = [].get(0)
mixed: list[num | err] = [1, bad]
Run in playground

The first explicit annotation establishes a persistent contract for the binding. Later writes must satisfy it. Repeating an equivalent annotation is allowed; replacing it with a broader, narrower, or unrelated contract is rejected with NLK2011. Union order is immaterial, including inside container contracts: list[num | str] and list[str | num] are equivalent.

Assignment, destructuring, subscript assignment, primed updates, and commit-loop state preserve the same contract. Use another binding name when a value needs a different contract.

Intentional failure — NLK2011 contract replacement
value: num = 1
value: num | str = "changed"

Known recursive incompatibility is NLK2011. Its diagnostic identifies the structural path and the expected and inferred types when available, such as dictionary value -> list element -> tuple item 2: expected bool, got str. Unknown shapes defer to authoritative recursive runtime validation.

def first_pair(xs: list[num]) -> tuple[num, num]:
return xs[0], xs[1]
left, right = first_pair([3, 4])
print(left + right)
Run in playground