Skip to main content

Concrete Hew programs

Small, focused programs covering basic functions, actors, supervision, and wire schemas. Each one opens in the browser playground.

Hello World

The smallest complete Hew program: one entry point and one line of output.

Open in Playground
hello_world.hew
fn main() {
    println("Hello, World!");
}

Fibonacci

Recursive functions, integer control flow, ranges, and formatted output.

Open in Playground
fibonacci.hew
fn fib(n: i64) -> i64 {
    if n <= 1 {
        n
    } else {
        fib(n - 1) + fib(n - 2)
    }
}
 
fn main() {
    for i in 0 .. 10 {
        println(f"fib({i}) = {fib(i)}");
    }
}

Counter Actor

A local actor owns its state and updates it through typed receive methods.

Open in Playground
counter_actor.hew
actor Counter {
    var count: i64;
    receive fn increment(n: i64) -> i64 {
        count = count + n;
        println(f"Count is now: {count}");
        count
    }
}
 
fn main() {
    let c = spawn Counter { count: 0 };
    // Each call waits for its handler; expect reports a failed call.
    c.increment(5).expect("counter stopped");
    c.increment(3).expect("counter stopped");
    let total = match c.increment(12) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    println(f"Final total: {total}");
}

Actor Pipeline

Spawn an actor and await a sequence of message calls from main.

Open in Playground
actor_pipeline.hew
actor Doubler {
    receive fn process(n: i64) -> i64 {
        let result = n * 2;
        println(f"Result: {result}");
        result
    }
}
 
fn main() {
    let doubler = spawn Doubler;
    let a = match doubler.process(5) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    let b = match doubler.process(21) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    let c = match doubler.process(100) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    println(f"got {a}, {b}, {c}");
}

Supervisor

A small supervision tree with restart strategy, restart budget, and named children.

Open in Playground
supervisor.hew
actor Worker {
    let id: i64;
    receive fn work(x: i64) -> i64 {
        println(f"Worker {id} processing {x}");
        x
    }
}
 
supervisor WorkerPool {
    strategy: one_for_one;
    intensity: 5 within 10s;
 
    child w1: Worker { id: 1 };
    child w2: Worker { id: 2 };
}
 
fn main() {
    let workers = spawn WorkerPool;
    // Access supervised children by their declared names.
    let w1 = workers.w1;
    let w2 = workers.w2;
    let a = match w1.work(10) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    let b = match w2.work(20) {
        .Ok(v) => v,
        .Err(_) => -1,
    };
    println(f"w1 -> {a}");
    println(f"w2 -> {b}");
}
// After both replies, the program exits and its supervised workers stop.

Wire Types

Tagged wire structs and enums for explicit serialization schemas.

Open in Playground
wire_types.hew
// Wire types are schema-checked serialization contracts. Each field carries
// an explicit tag (@1, @2, ...) so the binary encoding stays compatible as
// the schema evolves. Optional fields may be added or omitted across versions.
#[wire]
type UserMessage {
    name: string @1;
    age: i32 @2;
    nickname: Option<string> @3 optional;
}
 
fn main() {
    println("Wire types define serialization schemas.");
    println("Fields have explicit tags (@1, @2) for");
    println("backwards-compatible binary encoding.");
}