V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
moonbitlang
V2EX  ›  编程

本周更新: MoonBit 替换 Array 实现、键值对增加模式匹配支持

  •  
  •   moonbitlang · 14 天前 · 163 次点击

    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
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2876 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 14:38 · PVG 22:38 · LAX 07:38 · JFK 10:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.