Learn · 04

Variables, Mutability & Types

Learn let vs var, local type inference, explicit type annotations, primitive types, and type conversions in Ochre.

Ochre is statically typed with local type inference. Every variable has a single concrete type known at compile time, yet you rarely have to repeat obvious types.

let (Immutable) vs var (Mutable)

Bindings are immutable by default using let. When you genuinely need to update a variable over time (such as a counter or accumulator), declare it with var:

fn main() {
    let max_retries = 3          // inferred as Int (immutable)
    let rate: Float = 0.75       // explicit type annotation
    let enabled = true           // inferred as Bool

    var attempts = 0             // mutable Int
    attempts += 1
    attempts += 1

    println("max={max_retries}, rate={rate}, enabled={enabled}, attempts={attempts}")
}

Expected Output

max=3, rate=0.75, enabled=true, attempts=2

Why default to let? When reading a 60-line function, knowing that a binding can never change eliminates an entire class of mental bookkeeping — and in Ochre, let also prevents nested mutations on lists, maps, and structs.

Primitive Types

TypeExamplesNotes
Int0, -42, 1_000_00064-bit signed integer; underscores allowed for readability
Float3.14, -0.5, 1_000.2564-bit IEEE-754 floating point
Booltrue, falseBoolean truth values
Str"hi", 'raw'UTF-8 strings (interpolated or raw)
Void—Returned by statements/functions with no value

Explicit Type Conversions

Ochre does not perform implicit numeric coercion between Int and Float, or between Str and Int. This prevents subtle precision loss and unexpected string concatenation. Use the built-in conversion functions int(), float(), and str():

fn main() {
    let whole = 7
    let ratio = float(whole) / 2.0
    let parsed = int("42") + 8
    let label = "Result: " + str(parsed)

    println("ratio={ratio}, parsed={parsed}, label={label}")
    println("types: {type_of(whole)}, {type_of(ratio)}, {type_of(label)}")
}

Expected Output

ratio=3.5, parsed=50, label=Result: 50
types: Int, Float, Str

Type Annotations for Empty Collections

When initializing an empty list [] or channel channel(), add a type annotation so the type checker can enforce element types on every subsequent operation:

fn main() {
    let ids: List<Int> = []
    let updated = push(ids, 101)
    let more = push(updated, 102)
    println("ids={ids}, more={more}")
}

Expected Output

ids=[], more=[101, 102]

Common Mistakes & Compiler Diagnostics

1. Reassigning a let binding

fn main() {
    let count = 10
    count = 20
}

The compiler reports: cannot assign to immutable variable 'count' and advises declaring count with var.

2. Mixing Int and Float without conversion

fn main() {
    let x: Int = 5
    let y: Float = 2.5
    let z = x + y
}

The compiler reports: cannot apply '+' to 'Int' and 'Float'. Convert explicitly with float(x) + y.

Practical Use Case: Unit Conversion

Explicit numeric types shine in billing, physics, and metrics calculations where integer cents and floating-point percentages must never be accidentally mixed.

Exercise

Create an immutable price_cents: Int = 1999 and tax_rate: Float = 0.08. Compute the floating-point total in dollars ((float(price_cents) / 100.0) * (1.0 + tax_rate)) and print it.

Show Solution
fn main() {
    let price_cents: Int = 1999
    let tax_rate: Float = 0.08
    let dollars = float(price_cents) / 100.0
    let total = dollars * (1.0 + tax_rate)
    println("Subtotal: ${dollars}, Total: ${total}")
}