MoonBit 周报 Vol.41:替换 Array 实现、键值对增加模式匹配支持

MoonBit更新

  • 【Breaking Change】Array重命名为FixedArray@vec.Vec重命名为Array
// Before
fn init {
  let array : @vec.Vec[Int] = [1, 2, 3]
}
// After
fn main {
  let array : Array[Int] = [1, 2, 3]
}
  • 【语法】键值对(如Map HashMap等)增加模式匹配支持
    • 类型需实现op_get方法,其键为原生类型(Int Char String Bool等),值为Option[T]
    • 匹配时,键需为字面量
    • { "key": pat } 中,模式 pat 类型是 Option[T]None表示 "key"不存在,Some(p)表示 "key"存在,且 p 会被用于匹配这个键的值
    • 匹配键值对的模式都是开放的:未被匹配的键即使存在也会被忽略掉
    • 键值对模式会生成优化过的代码,每个键至多被查询一次
fn main {
  let map = @map.Map::[ ("a", 1) ]
  match map {
    // 当 `map` 包含 "b" 时匹配,
    // 并把 "b" 在 `map` 中的值绑定到 `y`
    { "b": Some(y) } => println(y)
    // 当 `map` 不包含 "b" 而包含 "a" 时匹配,
    // 并把 "a" 的值绑定到 `k`
    { "b": None, "a": Some(k) } => println(k)
    // 编译器提示 { "b": None, "a": None } 的情况未被匹配到
  }
  // 输出:1
}
  • 【语法】允许在已知类型信息的情况下省略newtype构造器
type A Int

pub fn op_add(self : A, other : A) -> A {
  self.0 + other.0 // 省略构造器
}

fn main {
  A::A(0) + 1 |> ignore // 省略 1 的构造器
  let _c : A = 0 + 1 + 2
}

构建系统更新

  • 配置文件选项统一为kebab-case(近期仍对snake_case保持兼容)
{
  "is-main": true,
  "test-import": []
}
  • 【Wasm,Wasm-GC】后端支持在moon.pkg.json中指定导出内存名称(默认为moonbit.memory)与编译选项(如-no-block-params以兼容binaryen工具链)
{ 
 "link": {
  "wasm": {
  "export-memory-name": "custom_memory_name",
  "flags": ["-no-block-params"]
 },
}
  • moon check 增加 --deny-warn 选项,在有 warning 时视为失败,返回非0值
  • moon fmt 增加 --check选项,用于检查当前代码是否已被格式化

标准库更新

  • 增加实验性库moonbitlang/x,用于开发与测试API不稳定的包。moonbitlang/x中的包稳定后,我们会根据社区的意见,选取重要的包合入moonbitlang/core
    • num time uuid json5 均已移动至moonbitlang/x
  • Bytes API 变更,从Int迁移到了Byte类型:
fn Bytes::op_get(self : Bytes, index : Int) -> Byte
fn Bytes::op_set(self : Bytes, index : Int, value : Byte) -> Unit
fn Bytes::length(self : Bytes) -> Int
fn Bytes::make(len : Int, ~init : Byte = b'\x00') -> Bytes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值