Aller au contenu principal

Programmes Hew concrets

Commencez avec de petits programmes ciblés qui montrent ce que le compilateur et le Playground actuels peuvent traiter : fonctions de base, acteurs, syntaxe de supervision, et schémas Wire.

Hello World

Le plus petit programme Hew complet : un point d’entrée et une ligne de sortie.

Ouvrir dans le Playground
hello_world.hew
fn main() {
    println("Hello, World!");
}

Fibonacci

Fonctions récursives, flot de contrôle sur les entiers, plages, et sortie formatée.

Ouvrir dans le 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

Un acteur local possède son état et le modifie avec des méthodes receive typées.

Ouvrir dans le 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

Démarrez un acteur puis attendez une séquence d’appels de messages depuis main.

Ouvrir dans le 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

Un petit arbre de supervision avec stratégie de redémarrage, budget, et enfants nommés.

Ouvrir dans le 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

Structs et enums Wire étiquetés pour des schémas de sérialisation explicites.

Ouvrir dans le 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.");
}