Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types

Primitives

TypeSemantics
Int64-bit signed, two’s complement, wrapping overflow.
FloatIEEE-754 binary64. NaN exists and NaN != NaN.
Booltrue or false.
StrImmutable Unicode text. Length and indexing count code points, not bytes or grapheme clusters.

There is no implicit Int ↔ Float conversion. 1 + 1.0 is a compile error.

Collections

TypeOrderLiteral
List<T>insertion order[1, 2, 3]
Map<K, V>sorted by K (Ord)none; build with map.empty()
Dict<V>insertion order, Str keys["a": 1, "b": 2], empty [:]
Set<T>sorted by Tnone; build with set.empty()

All collection operations return a new value. Indexing is partial and returns Option<T>:

xs[0]      // Option<T>, None when out of bounds
m[key]     // Option<V>
d["name"]  // Option<V>
s[1]       // Option<Str>, one code point

Map requires K: Eq + Ord. Its iteration order is the sorted key order, which keeps output identical across targets. Dict ignores order for equality but preserves it for display and iteration.

Structs and enums

A struct is a nominal record with named fields. An enum is a nominal sum type whose variants may carry payloads.

export struct User {
  name: Str
  age: Int
}

export enum Role {
  Admin
  Member(Int)
  Guest
}

Two structs with the same fields are still different types. A struct and an enum never unify. Type identity is nominal per loaded module; two packages with the same name in different dependencies are different types.

Widening with embed

A struct may embed exactly one other struct:

export struct TableInfo {
  name: Str
  columns: List<ColumnInfo>
}

export struct UsersTable {
  embed table_info: TableInfo
  id: IntColumn
}

UsersTable can be used where TableInfo is expected, because the embedded field has exactly that type. The relation is one step only and applies to arguments, returns, annotated bindings, and collection elements. It does not become covariance for List, Map, Dict, Option, or Result. The embedded field remains an ordinary field: construct it by name, read it as users.table_info, and compare it structurally.

Option and Result

Option<T>     // Some(T) | None
Result<T, E>  // Ok(T) | Err(E)

T? is sugar for Option<T>. Some, None, Ok, and Err are always in scope.

Functions as values

Function types are written fn(A, B) -> R. A lambda is fn(x: Int) => x + 1; parameter types may be inferred from context.

  • A function value can live in a local, parameter, return value, or package binding, and is called as f(x).
  • Functions cannot be stored in struct fields, enum payloads, or collections.
  • Functions cannot be compared or converted with to_string.
  • A lambda captures values; later mutations of an outer let mut are invisible inside it.
  • A generic function cannot be used as a value. Wrap it in a concrete lambda.

Generics

Generics are available on structs, enums, and functions. Constraints are limited to the built-in traits Eq, Ord, and Display; Map<K, V> requires K: Eq + Ord. There are no higher-kinded types, associated types, or default type parameters.

package generic_fn {
  export fn identity<T>(value: T) -> T {
    value
  }

  export fn first<T>(items: List<T>) -> Option<T> {
    items.get(0)
  }

  test "infers from a scalar" {
    expect.eq(identity(7), 7)
  }

  test "infers from a list" {
    expect.eq(first(["a", "b"]), Some("a"))
  }
}

Complete example

package map_case {
  export struct Point {
    x: Int
    y: Int
  }

  test "inserts and reads" {
    let empty: Map<Str, Int> = map.empty()
    let filled = empty.insert("b", 2).insert("a", 1)
    expect.eq(filled.get("a"), Some(1))
    expect.eq(filled.len(), 2)
    expect.eq(filled.keys(), ["a", "b"])
    expect.eq(empty.len(), 0)
  }

  test "struct keys follow ord" {
    let empty: Map<Point, Str> = map.empty()
    let filled = empty.insert(Point(x: 10, y: 0), "ten").insert(Point(x: 2, y: 0), "two")
    expect.eq(filled.keys(), [Point(x: 2, y: 0), Point(x: 10, y: 0)])
  }

  test "removes" {
    let empty: Map<Str, Int> = map.empty()
    expect.eq(empty.insert("a", 1).remove("a").len(), 0)
  }
}