MoonBit 周报 Vol.34:支持 expect testing 与多参数构造器

构建系统更新

1. 支持 expect testing

a. 使用 moon new 新建一个 MoonBit 项目。

b. 在 lib/hello.mbt中写入:

rust 复制代码
pub fn hello() -> String {
  "Hello, world!"
}

test {
  let buf = Buffer::make(10)
  buf.write_string(hello())
  buf.expect()?
}

c. 然后运行 moon test --update或者 moon test -u:

shell 复制代码
$ moon test --update
expect test failed at lib/hello.mbt:8:3-8:15
Diff:
----
Hello, world!
----

Total tests: 1, passed: 0, failed: 1.

Auto updating expect tests and retesting ...

Total tests: 1, passed: 1, failed: 0.

d. 再次打开 lib/hello.mbt 文件,可以看到已经将测试结果 promote 到源码中。

rust 复制代码
pub fn hello() -> String {
  "Hello, world!"
}

test {
  let buf = Buffer::make(10)
  buf.write_string(hello())
  buf.expect(~content="Hello, world!")?
  //         ^~~~~~~~~~~~~~~~~~~~~~~~ 测试结果更新
}

2. moon run 不再支持 --output-wat选项。

MoonBit 更新

1. 支持多参数构造器的后端代码生成

支持多参数构造器(multi-argument constructor)的后端代码生成。现在构造一个泛型类型的值的时候,如果泛型参数为元组的话必须要写括号,即:

rust 复制代码
enum A[X] {
  A(X)
}

fn init {
  // Error, expecting 1 arg, getting 2
  A::A(1, 2) |> ignore

  // Ok
  A::A((1, 2)) |> ignore
}

多参数构造器 unbox 了参数,能够提高生成的代码的性能,也允许程序员对数据的内存布局有了更多的掌控。

2. 调整了Int64的lsl, lsr, asr方法

现在移位参数不再是Int64,而是Int。同时调整了clz, ctz, popcnt方法,现在返回类型不再是Int64,而是Int。此项改变有助于我们在不支持原生Int64的平台上生成更高效的代码。

IDE 更新

1. 支持带标签参数的重命名。

2. VSCode 插件支持自动安装或者更新 MoonBit

a. 更新插件后,如果没有安装 moon 或者 moon 不是最新的时候,VSCode 右下角弹出自动安装/升级的提示。

b. 点击 "yes", 来执行自动安装任务。任务完成后就可以用了。

相关推荐
大模型教程2 小时前
全网首发!清北麻省顶级教授力荐的《图解大模型》中文版终于来了,碾压 95% 同类教材
程序员·llm·agent
AI大模型2 小时前
谷歌 Agents 白皮书中文版全网首发,堪称 AI 教材的天花板级神作
程序员·llm·agent
阿里嘎多学长5 小时前
2025-12-15 GitHub 热点项目精选
开发语言·程序员·github·代码托管
用户23452670098220 小时前
Python构建AI Agent自主智能体系统深度好文
后端·程序员
Gopher21 小时前
逝水流远,长忆当歌——我的2025
程序员
文心快码BaiduComate1 天前
给 AI 装上“员工手册”:如何用Rules 给文心快码 (Comate) 赋能提效?
前端·程序员·前端框架
程序员鱼皮1 天前
全面封禁 Cursor!又一家大厂出手了
程序员·ai编程·cursor
大模型教程1 天前
这份中国人写的大模型书,在外网竟然被刷爆了!
程序员·llm·agent
大模型教程1 天前
大模型入门指南 - Training:小白也能看懂的“模型训练”全解析
程序员·llm·agent