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

Cambrian is a statically typed language. Every variable, parameter, member, and expression has a type that is known at compile time. The type system emphasises explicit widths, checked vs wrapping arithmetic, and clear address / collection types so state transitions stay predictable across backends.

Type Categories

Cambrian types fall into several categories:

CategoryExamplesChapter
Primitivesbool, u64, U256, addressPrimitive Types
GenericsVec<T>, HashMap<K,V>, Option<T>Generic Types
Recordsrecord Pool { ... }Records
Enumsenum Status { Active, Paused }Enums
Type Aliasestype Amount = U256Type Aliases
Tuples(u64, address), (bool, U256, u8)See below

Tuples

Cambrian supports tuple types for grouping a fixed number of values:

pure fn split(total: u64) -> (u64, u64) {
    let half = total / 2;
    (half, total - half)
}

Tuples are positional – their elements are read by destructuring in let bindings:

let (lo, hi) = split(total);

Tuples are supported only as multi-value return types and in let destructuring. They cannot be used as member types or collection elements (Vec<(u64, address)>); define a record instead.

Type Annotations

Type annotations appear after a colon in function parameters, route parameters, member declarations, and return types:

pure fn add(a: u64, b: u64) -> u64 {
    a + b
}

let bindings in routes and blocks do not take a type annotation; their type is inferred from the right-hand side. Use a cast or a typed parameter when you need a specific width.

Type Casts

The as keyword performs explicit type conversion between compatible types:

let big = small as u64;

Type casting is especially common when working with mixed-width integer arithmetic. See Arithmetic and Logic for details on checked and wrapping operations across types.