关于程序员:MoonBit支持云原生调试功能

4次阅读

共计 1710 个字符,预计需要花费 5 分钟才能阅读完成。

MoonBit 更新

1. 反对云原生调试性能

当初,你能够通过拜访 try.moonbitlang.com,间接在浏览器中应用 devtools 调试 MoonBit 程序,无需装置任何软件。具体的应用步骤如下:

2. MoonBit 反对应用 for 关键字定义的函数式循环控制流

MoonBit 当初反对应用 for 关键字定义的函数式循环控制流,其性能靠近于 C/C++ 等底层语言,比方 fib 函数能够写成如下模式:

fn fib(n : Int) -> Int {
    for i = 0, a = 1, b = 2
        i < n
        i = i + 1, a = b, b = a + b {} else { b}
}

MoonBit 的 for 循环能够作为表达式返回一个值,比方上述程序中在循环完结后应用 b 作为整个 for 循环的值,也能够在 for 的循环体中通过 break 提前返回,比方:

fn exists(haystack: Array[Int], needle: Int) -> Bool {for i = 0; i < haystack.length(); i = i + 1 {if haystack[i] == needle {break true}
  } else {false}
}

此外,在 for 循环中能够像传统语言一样应用 continue 进入下一次循环,MoonBit 额定提供了带参数的 continue 来指定下一次循环过程中循环变量的值,比方:

fn find_in_sorted[T](xs: Array[(Int, T)], i: Int) -> Option[T] {for l = 0, r = xs.length() - 1; l < r; {let mid = (l + r) / 2
    let k = xs[mid].0
    if k == i {break Some(xs[mid].1)
    } else if k > i {continue l, mid} else {continue mid + 1, r}
  } else {None}
}

在不须要返回值的状况下,else 分支能够省略,比方:

fn print_from_0_to(n: Int) {
  for i = 0; i <= n; i = i + 1 {println(i)
  }
}

3. Inline test 改良

测试的返回类型从 Unit 改成了Result[Unit,String],用于示意测试的后果:

 test "id" {if (id(10) != 10) {return Err("The implementation of `id` is incorrect.") }
    }

编译器会主动将 test "id" {...} 的语句块{...} 应用 Ok() 包裹起来。因而,当语句块的类型为 Unit 并且没有提前 return 时,示意 inline test 测试通过。配合问号操作符,能够让测试变得更加优雅:

fn id(x : Int) -> Int {x + 1 // incorrect result}
   
   fn assert(x : Bool, failReason : String) -> Result[Unit,String] {if x { Ok(()) } else {Err(failReason) }
   }
   
   test "id" {assert(id(10) == 10, "The implementation of `id` is incorrect.")?
   }

执行moon test,输入如下:

➜  my-project moon test
running 1 tests in package username/hello/lib
test username/hello/lib::hello ... ok
   
test result: 1 passed; 0 failed
   
running 1 tests in package username/hello/main
test username/hello/main::id ... FAILED: The implementation of `id` is incorrect.
   
test result: 0 passed; 1 failed
   
Hello, world!

4. 改良 VS Code 插件的函数签名提醒,当初会显示参数名:

5. 改良了 VS Code 插件对 core 包开发的反对

6. moon new 反对疾速创立新我的项目

  • moon new hello 在文件夹 hello 中创立一个名为 username/hello 的可执行我的项目
  • moon new hello --lib 在文件夹 hello 中创立一个名为 username/hello 的模块
正文完
 0