跳到主要内容

Hew

Hew 将基于 actor 的程序编译为原生代码。每个 actor 拥有自己的状态,supervisor 决定失败的 actor 如何重启。

阅读当前状态 · GitHub

状态留在 actor 内

这个 session actor 拥有自己的消息 vector。只有 post 会修改它;每次调用都会等 handler 完成后再返回 Result。

count 读取同一份私有状态并把值返回给 caller。错误分支保留在代码中,因为 actor 可能在调用完成前停止。

阅读 actor 模型 → EN
session.hew
// A chat session is an actor: state lives in fields and only its handlers
// can mutate it. Ordinary actor calls wait for the handler and return a
// Result, whether or not the handler itself returns a value.
actor Session {
    let user: string;
    var messages: Vec<string> = Vec.new();
    receive fn post(text: string) {
        messages.push(text);
    }
    receive fn count() -> i64 {
        messages.len()
    }
}
 
fn main() {
    let chat = spawn Session { user: "ada", messages: [] };
    chat.post("hello").expect("actor stopped");
    chat.post("world").expect("actor stopped");
    let total = chat.count();
    match total {
        .Ok(n) => println(f"{n} messages posted"),
        .Err(_) => println("session unavailable"),
    }
}

安装 Hew

选择操作系统和编译器版本。

v0.9.3 · 发布版本

阅读完整安装指南。

语言明确表达的内容

v0.9.3 中可用的 actor、监督、并发和 wire 功能。

调用 Session
  1. 调用方: chat.post("hello"). 等待处理函数的结果.
  2. 邮箱: post("hello").
  3. 一次处理函数执行: messages.push(text). 私有状态: messages: ["hello"].
  4. 调用结果 → 调用方.

一次成功的 post 调用会先更新 Session 的私有状态,再返回结果。

Actor 隔离

每个 actor 都独占自己的状态。编译器会强制执行本地 actor 边界和 Send 检查。

监督树

服务会崩溃——Hew 预期这一点。Supervisor actors、重启预算和核心重启策略是语言本身的一部分,而不是独立框架。

结构化并发

scope 限定子任务的生命周期,并在返回前等待它们完成;fork 启动可在之后汇合的独立 actor 调用。

Wire 契约

Wire types 将 schema、字段标签和兼容性检查放进语言中,并与发送它们的 actors 并列。

工程日志

Hew 0.6.2: safer values, steadier services Edit parsed configuration without changing its source value, then see how Hew 0.6.2 carries the same actor reference across local and remote services. 2026年10月21日
阅读博客 → EN

Hew Notes

Language development, engineering articles, releases and community projects. About once a month, with an extra issue for a significant release. Written in English.

Subscribe to Hew Notes