跳到主要内容

weekly 2024-01-15

· 阅读需 3 分钟

MoonBit更新

1. 放宽了match的右手侧的语法,允许单个语句的出现。现在允许下面的写法:

match x {
A => return
B => return 1
C => i = i + 1
D => break
E => continue
F => while true {}
}

2. 修复formatter的各种bug,例如:

源代码 修复前 修复后
fn init {
let mut a = 1
{
{
let a = 2
f(a)
}
let a = 3
f(a)
{
let a = 4
f(a)
}
}
f(a)
}
fn init {
let mut a = 1
let a = 2
f(a)
let a = 3
f(a)
let a = 4
f(a)
f(a)
}
fn init {
let mut a = 1
{
{
let a = 2
f(a)
}
let a = 3
f(a)
let a = 4
f(a)
}
f(a)
}

3. 新增实验性inline测试机制

声明格式为fn test_*,inline测试需要在普通的 *.mbt 文件中(而不是 *_test.mbt)声明,它既不能有参数也不能有返回类型,例如以下写法会报错:

现在 moon test 除了会执行每个包中以 _test.mbt 结尾的测试文件,还会执行每个包中的 inline 测试。

构建系统更新

1. moon new给各个选项增加了默认值,用户可以使用回车选择默认值

$ moon new
Enter the path to create the project (. for current directory) [default: myproject] >
Enter the create mode (exec or lib) [default: exec] >
Enter your username [default: username] >
Enter the package name [default: hello] >
Enter your license [default: Apache-2.0] >
Created my-project

2. moon.mod.json增加license和repository字段。

license表示这个mooncakes.io所使用的licencse,必须符合spdx标准。

3. 正式移除moon check --daemon

4. moon publish新增上传大小限制,上传大小必须小于16Mib

其他更新

1. windows平台的安装路径从~/.moon改为~/.moon/bin,与其他平台保持一致。

2. 修复关于newtype goto definition 和 rename 的 bug

weekly 2024-01-08

· 阅读需 2 分钟

MoonBit更新

1. 正式移除 interface 关键字

正式移除了 interface 关键字,使用 trait 代替。

trait

2. 引入let mut id = expr

根据社区的反馈,引入let mut id = expr的语法替代 var id = expr,下周将移除 var id = expr的支持。

mut

3. 给 Array 类型增加了 Default 的实现

例如:

fn init {
debug(Array::default()) // []
}

4. 给 List 类型增加了 DefaultEq、和Debug 的实现

例如:

fn init {
let l1: List[_] = Cons(1, Cons(2, Cons(3, List::default())))
let l2: List[_] = Cons(1, Cons(2, Cons(4, Nil)))
debug(l1) // Cons(1, Cons(2, Cons(3, Nil)))
debug(l1 == l2) // false
debug(l2 == l2) // true
}

5. 修复对pub函数体的类型检查

形如这样的例子:

priv type T
pub fn f() {
let t: T = ... // public definition cannot depend on private type
...
}

之前会在T报错,但现在不会了。

插件更新

1. 新增MoonBit AI

目前已新增MoonBit AI,地址是https://ai.moonbitlang.com,欢迎大家试用。

2. 提高LSP稳定性

修复一些会导致LSP崩溃的bug,提升LSP的稳定性。

构建系统更新

1. 修复 moon test 会测试 .mooncakes 文件夹下的包的问题

2. 废弃 moon check --daemon

3. 改进 moon.pkg.json 格式或内容错误时的错误提示

如下图所示: build-system

weekly 2024-01-02

· 阅读需 8 分钟

MoonBit更新

1. 上线了MoonBit包管理平台 mooncakes.io

详细信息见:https://mp.weixin.qq.com/s/dBA4dA2fKL4FHc6KOcisBg

2. ⽀持了 recursive newtype

可以在MoonBit中实现类型安全的y combinator:

type R[X] (R[X]) -> X

fn y[X, Y](f : ((X) -> Y) -> (X) -> Y) -> (X) -> Y {
fn ff (x: R[(X) -> Y]) -> (X) -> Y {
fn(a) { f(x.0(x))(a) }
}
ff(R::R(fn(x) { fn (a) { f(x.0(x))(a) } }))
}

fn factx(f: ((Int) -> Int)) -> (Int) -> Int {
fn(n: Int) -> Int {
if n <= 1 { 1 } else { n * f(n-1)}
}
}

fn init {
let fact = y(factx)
let n = fact(10)
println(n)
}

3. 新增内置函数sqrt

用于计算二次方根

fn init {
// sqrt 的类型是 Double -> Double
println(sqrt(4.0)) // 2.0
}

4. 新增运算符 ===

用于判断两个值是否引用相等:

fn init {
let x = [1, 3]
let y = [1, 3]
let z = x
if x === y {
println("x === y")
} else if x === z {
println("x === z")
}
// Output: x === z
}

5. method/trait系统的更新:

在过去的几周里,我们对 MoonBit 的方法/接口系统进行了许多设计上的调整,让它的行为更加合理、健壮。下面是现在的方法系统的行为:

  • 方法是和类型关联的函数。可以通过下面的语法定义一个方法
fn T::method(...) -> ... { ... }

// 例如
type MyInt Int
fn MyInt::default() -> MyInt { MyInt(0) }

enum MyList[X] {
Nil
Cons(X, MyList[X])
}

fn MyList::map2[X, Y, R](
f: (X, Y) -> R,
xs: MyList[X],
ys: MyList[Y]
) -> MyList[R] {
...
}

作为一种便捷的语法糖,当函数的第一个参数名为 self 时,Moonbit 会自动将它定义成 self的类型上的方法:

fn add(self: MyInt, other: MyInt) -> MyInt { ... }
// 等价于
fn MyInt::add(x: MyInt, y: MyInt) -> MyInt { ... }
  • 方法都是普通函数。所以在没有歧义时,可以直接当成普通函数调用:
enum MyList[X] { ... }
fn MyList::length[X](xs: MyList[X]) -> Int {
...
}

fn init {
let xs: MyList[_] = ...
debug(length(xs))
}

如果有歧义无法直接调用,也可以用 T::method(...) 的形式显式调用:

struct Type1 { ... } derive(Debug)
fn Type1::default() -> Type1 { ... }

struct Type2 { ... } derive(Debug)
fn Type2::default() -> Type2 { ... }

fn init {
// debug(default()): 有歧义!
debug(Type1::default()) // ok
debug(Type2::default()) // ok
}
  • 当方法的第一个参数就是它所属的类型时,可以使用 x.method(...) 语法来快捷地调用。而且这种调用方式在跨包时不需要写出包名。MoonBit 会自动根据 x 的类型找到正确的方法:
// 名为 @list 的包
pub enum List[X] { ... }
pub fn map[X](self: List[X], f: (X) -> Y) -> List[Y] {
...
}

// 在另一个包中使用 @list
fn init {
let xs: @list.List[_] = ...
// 下面三种写法是等价的
xs.map(...)
@list.map(xs, ...) // 无歧义时可以如此调用
@list.List::map(xs, ...)
}
  • **只有类型所在的包可以给类型定义方法。**这保证了第三方包无法意外或恶意修改现有类型的行为和 trait 系统的一致性。

MoonBit 的 trait 系统的行为变化如下:

  • trait 中的方法声明,任何时候都不再需要 Self:: 前缀。方法的第一个参数是否是 Self 对行为不再有影响

  • 类型可以通过它现有的方法自动地实现一个 trait,不需要手动写出。但如果一个类型没有实现一个 trait,或者原本的实现不能满足需求,需要拓展它的功能,可以用如下的语法定义特殊的拓展方法,用于给一个类型显式地实现某个 trait

// 给 [T] 实现 trait [Eq] 中的 [op_equal] 方法
fn Eq::op_equal(x: T, other: T) -> { ... }

这些拓展方法只能用于实现指定的 trait。例如,上面的拓展方法 Eq::op_equal 只能被用于实现 Eq,不能被用 T::op_equal 或是 t.op_equal(...) 的形式直接调用。在寻找 trait 的实现时,拓展方法的优先级比普通方法高。

  • 只有类型所在的包或 trait 所在的包可以定义拓展方法。因此,某个类型为某个 trait 提供的实现在任何地方都是唯一且确定的。这保证了第三方包不会意外地更改一段程序的行为。

和之前相比,方法/trait 系统最大的不兼容改动是,现在不能给内建和第三方类型直接定义方法了。但通过心得拓展方法的机制,依然可以为内建/第三方类型实现新的 trait 来拓展功能。

构建系统更新

1. moon.pkg.jsonimport 字段增加了数组的表示

数组中要么是一个字符串,要么是一个 object { "path": ..., "alias": ...},比如:

{
"is_main": true,
"import": [
{ "path": "moonbitlang/import004/lib", "alias": "lib" },
"moonbitlang/import004/lib2", // 使用默认的alias: "lib2"
{ "path": "moonbitlang/import004/lib3", "alias": "lib3" },
{ "path": "moonbitlang/import004/lib4", "alias": "" } // 使用默认的alias: "lib4"
]
}

2. moon new现在支持通过交互式方式来创建项目。

  • 创建一个可执行项目
$ moon new
Enter the path to create the project (. for current directory) > myproject
Enter the create mode (exec or lib) > exec
Enter your username > moonbitlang
Enter the package name > hello

上面的命令等价于

 moon new --path myproject --user moonbitlang --name hello

这将会在文件夹 ./myproject 中创建一个名为 moonbitlang/hello 的项目,其目录结构为

.
├── lib
│ ├── hello.mbt
│ ├── hello_test.mbt
│ └── moon.pkg.json
├── main
│ ├── main.mbt
│ └── moon.pkg.json
└── moon.mod.json
  • 创建一个包
$ moon new
Enter the path to create the project (. for current directory) > mylib
Enter the create mode (exec or lib) > lib
Enter your username > moonbitlang
Enter the package name > hello

上面的命令等价于

 moon new --path mylib --lib --user moonbitlang --name hello

这将会在文件夹 ./mylib 中创建一个名为 moonbitlang/hello 的包,其目录结构为

.
├── lib
│ ├── hello.mbt
│ ├── hello_test.mbt
│ └── moon.pkg.json
├── moon.mod.json
├── moon.pkg.json
└── top.mbt

weekly 2023-12-25

· 阅读需 3 分钟

MoonBit更新

01. 添加内置类型 Result

enum Result[T, E] {
Ok(T)
Err(E)
}

02. 添加问号操作符

新增了问号操作符,用于简化错误处理:

fn may_fail() -> Option[Int] { ... }

fn compose_may_fail() -> Option[String] {
let x = may_fail()?
let y = may_fail()?.lsr(3)
Some ((x + y).to_string())
}

问号操作符的语义是:如果 t?t 的结果是 None,那么 t? 相当于 return None(直接跳出当前函数)。如果 t?t 的结果是 Some(x),那么 t? 相当于 x。除了 Option 类型,问号操作符也能作用在 Result 类型上:

fn may_error() -> Result[Int, String] { ... }

fn compose_may_error() -> Result[Int, String] {
let x = may_error()?
let y = may_error()?
if y == 0 {
return Err("divide by zero")
}
Ok (x / y)
}

03. 将关键字 interface 修改为 trait

根据社区的反馈,将关键字 interface 修改为 trait,暂时保留对关键字interface的兼容:

04. 修改死代码消除的逻辑

对于顶层 let 在没有被使用的情况下,一律视作可以被删除,无论其是否有副作用。比如,

let a = 1       // will be removed
let b: T = f(a) // will be removed
fn init {
.. // a and b are not used
}

如果函数 f 会触发副作用,需要把 f(a) 这个调用放到 fn init { .. } 中以避免其被删除

05. 修复代码格式化工具不能正确处理中文注释的问题

06.修复不能正确处理中文全局标识符的问题

IDE更新

01. 添加是否自动启动 moon check 的选项

vscode用户可以在settings.json中使用moonbit.autoMoonCheck.enable控制是否自动启动moon check

或者你也可以在设置中搜索MoonBit:

02. 修复 derive(Show) 误报错误的问题

修复前

修复后

构建系统更新

01. 添加 moon doc 命令

添加 moon doc 命令,用于生成和预览文档。moon doc --serve会根据代码中的markdown注释生成文档,并在本地启动网页服务器。访问输出的链接即可查看效果。