enum E { C(mut ~x : Int, mut ~y : Int) } derive(Debug) fn swap_xy(x : E) -> Unit { match x { // `~y` 会绑定模式匹配前 `C` 中的 `y` 字段的值 // 当看到 `C(..) as c` 这种模式时,编译器知道 `c` 一定是构造器 `C`, // 所以可以在分支内部用 `c.x`、`c.y` 直接访问 `C` 的字段 C(~y, ..) as c => { // `c.x` 和 `c.y` 可以用于修改/读取 `C` 中最新的值 c.y = c.x c.x = y } } } fn init { let e : E = C(x=1, y=2) debug(e) // C(x=1, y=2) swap_xy(e) debug(e) // C(x=2, y=1) }
这个例子中的 swap_xy
函数对构造器 C
的 x
和 y
两个 field 的值进行了交换,并且这个交换是 in-place 的,没有引入额外的内存分配
fn init { let v = [1, 2, 3] // v has type @vec.Vec[Int] let a : Array[_] = [1, 2, 3] // a has type Array[Int] }
接下来两周,我们计划将 Array
重命名为 FixedArray
,将 Vec
重命名为 Array
,以保证准确反映数据结构的特性,避免与向量混淆。
./vec/sort.mbt:68:16-68:23 [E4021] The value identifier minimum is unbound. ./vec/sort_by.mbt:90:16-90:23 [E4021] The value identifier minimum is unbound. ./vec/vec.mbt:962:37-962:50 [E4020] Package "iter" not found in the loaded packages. ./vec/vec.mbt:963:3-963:13 [E4024] The type/trait @iter.Iter is not found.
moon.pkg.json
中如下配置,可在编译时关闭对应的 warn 与 alter [其中 2
与 alter_1
、alert_2
分别为编译器预设的 warn id (对于 Unused variable) 与用户自定义的 alert id ] { "warn_list": "-2", "alert_list": "-alert_1-alert_2" }
-
代表关闭对应 id 的 warn 与 alter ,可用 moonc build-package -warn-help
查看预设的 warn
moon check|build|run|test
的默认后端由wasm
切换为wasm-gc
moon test
默认执行模式由 release
修改为 debug
moon check|build|run|test
支持自动安装依赖,而无需手动执行 moon install
moon doc --serve
支持指定地址与端口
-b, --bind <BIND> [default: 127.0.0.1] -p, --port <PORT> [default: 3000]
平台 | 优化前 | 优化后 |
---|---|---|
macOS arm64 | 7.3 MiB | 3.6 MiB |
macOS x86_64 | 8.2 MiB | 4.1 MiB |
Ubuntu x86_64 | 14.0 MiB | 9.6 MiB |
Windows x86_64 | 9.4 MiB | 4.9 MiB |
moon new hello
创建一个新项目,将 main/main.mbt
的内容替换为: fn bar() -> Unit { abort("") } fn foo() -> Unit { bar() } fn main { foo() }
执行 moon run main --debug
,可观察到输出类似:
error: RuntimeError: unreachable at $username/hello/main.bar.fn/1 (wasm://wasm/6fe99e5e:wasm-function[3]:0xe6) at $username/hello/main.foo.fn/2 (wasm://wasm/6fe99e5e:wasm-function[4]:0xea) at *init*/3 (wasm://wasm/6fe99e5e:wasm-function[5]:0xef) at <anonymous>:9:22