본문으로 건너뛰기

구체적인 Hew 프로그램

기본 함수, 액터, 감독 문법, 와이어 스키마처럼 현재 컴파일러와 Playground가 처리할 수 있는 내용을 보여주는 작고 집중된 프로그램으로 시작하세요.

Hello World

하나의 진입점과 한 줄 출력으로 이루어진 가장 작은 완전한 Hew 프로그램입니다.

Playground에서 열기
hello_world.hew
fn main() {
    println("Hello, World!");
}

Fibonacci

재귀 함수, 정수 제어 흐름, 범위, 형식화된 출력을 다룹니다.

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

로컬 액터가 자신의 상태를 소유하고 타입이 지정된 receive 메서드로 업데이트합니다.

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하고 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

재시작 전략, 재시작 예산, 이름 있는 자식이 있는 작은 감독 트리입니다.

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

명시적인 직렬화 스키마를 위한 태그가 있는 와이어 struct와 enum입니다.

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