2024-12-30
语言更新
- 新增labeled loop语法,可在多层循环中直接跳转到指定的某一层,label使用
~作为后缀
fn f[A](xs : ArrayView[A], ys : Iter[Int]) -> @immut/list.T[(Int, A)] {
l1~: loop 0, xs, @immut/list.Nil {
_, [], acc => acc
i, [x, .. as rest], acc =>
for j in ys {
if j == i {
continue l1~ i + 1, rest, @immut/list.Cons((j, x), acc)
}
if j + i == 7 {
break l1~ acc
}
} else {
continue i - 1, rest, acc
}
}
}
- 新增discard argument, 以单个下划线命名的函数参数会被丢弃,同一函数内可以丢弃多个参数。Discard argument 只支持位置参数,暂不支持命名参数。
fn positional(a : Int, b : Int) -> Int {
a + b
}
fn discard_positional(_: Int, _: Int) -> Int {
1
}
-
pub的语义正式从完全公开切换为只读,同时pub(readonly)语法被 deprecate。如果想要声明一个完全公开的type/struct/enum,需要写pub(all),如果想要声明一个完全公开(外部可以实现)的trait,需要写pub(open)。这一改动已提前通过 warning 的形式进行预告,之前使用 pub 会报 warning。如果已经按照 warning 将pub改为pub(all)/pub(open),本次只需将pub(readonly)改为pub即可完成迁移。moon fmt能自动完成pub(readonly) -> pub的迁移。 -
未来,
trait找不到实现时fallback到方法的行为 有可能 被移除。我们鼓励新代码使用显式的impl Trait for Type语法而非方法来实现 trait。在无歧义时,impl Trait for Type with method(...)也可以使用 dot syntax 调用,因此使用显式impl不会损失使用的便利性。 -
移除了带标签参数旧的前缀语法。
-
移除了用于获取 newtype 内容的旧语法
.0。同时如果一个 newtype 内的类型是 tuple,现在.0、.1这些 index access 能自动转发到 newtype 内的 tuple,例如:
type Tuple (Int, String)
fn main {
let t = (4, "2")
println(t.0) // 4
println(t.1) // 2
}
-
为
derive(FromJson)和derive(ToJson)加入了参数支持,可以控制类型序列化和反序列化过程的具体行为和数据布局。具体修改见 Docs > Language > Deriving。