MoonBit更新
【Breaking Change】Array
重命名為FixedArray
,@vec.Vec
重命名為Array
fn init { let array : @ vec. Vec [ Int ] = [ 1 , 2 , 3 ]
}
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 { { "b" : Some ( y) } => println ( y) { "b" : None , "a" : Some ( k) } => println ( k) }
}
【語法】允許在已知類型信息的情況下省略newtype
構造器
type A Int pub fn op_add ( self : A , other : A ) -> A { self . 0 + other.0
} fn main { A :: A ( 0 ) + 1 | > ignore 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