Learn · 13

Concurrency & Channels

Run parallel tasks with spawn and join, and coordinate safely across threads using typed Channel<T> queues without async/await coloring.

Ochre implements colorless concurrency: any function can launch parallel work with spawn { ... }, await its typed result with join(task), and stream messages through Channel<T> — without splitting your program into async and sync functions.

Spawning Typed Tasks (Task<T>)

spawn { ... } launches a background task and returns a Task<T> handle. The type checker infers T automatically from both explicit return statements and implicit tail expressions:

fn compute(n: Int) -> Int {
    return n * n
}

fn main() {
    let t1 = spawn {
        return compute(12)
    }
    let t2 = spawn {
        compute(15)
    }
    let total: Int = join(t1) + join(t2)
    println("total={total}")
}

Expected Output (CLI)

total=369

Communicating Over Typed Channels (Channel<T>)

Create a channel with let ch: Channel<Int> = channel(). Producers call send(ch, val) and finish with close(ch). Consumers call recv(ch), which returns Some(val) for each message and None once the channel is closed and drained:

fn main() {
    let ch: Channel<Str> = channel()

    let worker = spawn {
        sleep(10)
        send(ch, "job-1 done")
        send(ch, "job-2 done")
        close(ch)
        return 2
    }

    var running = true
    while running {
        match recv(ch) {
            Some(msg) => println("received: {msg}"),
            None => {
                running = false
            },
        }
    }

    let processed: Int = join(worker)
    println("worker finished {processed} jobs")
}

Expected Output (CLI)

received: job-1 done
received: job-2 done
worker finished 2 jobs

Common Mistakes & Compiler Diagnostics

1. Sending the wrong type on a typed channel

fn main() {
    let ch: Channel<Int> = channel()
    send(ch, "not an int")
}

The compiler catches this statically: cannot send 'Str' on 'Channel<Int>'.

2. Conflicting or partial return paths inside spawn

fn main() {
    let t = spawn {
        if true {
            return 1
        } else {
            return "two"
        }
    }
}

The compiler reports: conflicting return types in 'spawn' block: expected 'Int', found 'Str'. A spawn block that returns a value on one branch and falls through to Void on another is also rejected statically.

Practical Use Case: Parallel Fan-Out

Spawn one task per independent unit of work (such as hashing files, checking URLs, or computing partitions), collect their handles, and join them to aggregate results cleanly.

Exercise

Write a CLI program that spawns two tasks: one computing the sum of 1..50 and another computing the sum of 50..101. Join both tasks and verify the combined sum equals 5050 with assert.

Show Solution
fn sum_range(start: Int, end: Int) -> Int {
    var acc = 0
    for i in start..end {
        acc += i
    }
    return acc
}

fn main() {
    let a = spawn { return sum_range(1, 50) }
    let b = spawn { return sum_range(50, 101) }
    let total = join(a) + join(b)
    assert(total == 5050)
    println("total={total}")
}