跳到主要内容

具体的 Hew 程序

从小而聚焦的程序开始,看看当前编译器和 Playground 能处理什么:基础函数、actors、supervision 语法和 wire schemas。

Hello World

最小的完整 Hew 程序:一个入口点和一行输出。

在 Playground 中打开
hello_world.hew
fn main() {
    println("Hello, World!");
}

Fibonacci

递归函数、整数控制流、ranges 和格式化输出。

在 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

本地 actor 拥有自己的状态,并通过带类型的 receive methods 更新它。

在 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 一个 actor,并从 main await 一系列消息调用。

在 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

一个小型监督树,包含重启策略、重启预算和命名 children。

在 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

用于显式序列化 schemas 的带标签 wire structs 和 enums。

在 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.");
}