20251014 MoonBit Monthly Update Vol.04
Version v0.6.29β
Language Updatesβ
-
Asynchronous Programming Enhancements
Added support for
async test
andasync fn main
syntax, enabling asynchronous tests and asynchronous main functions. Bothasync fn main
andasync test
are based on themoonbitlang/async
library and are currently supported on the native backend for Linux and macOS. For more details about async programming in MoonBit, see the documentation and GitHub repository ofmoonbitlang/async
.Async tests
declared with async test will run in parallel.///| async test "http request" { let (response, result) = @http.get("https://www.example.org") inspect(response.code, content="200") assert_true(result.text().has_prefix("<!doctype html>")) }
-
Experimental Feature:
lexmatch
ExpressionAdded the
lexmatch
expression (experimental feature), which enables pattern matching onStringView
orBytesView
using regular expressions.The following example matches two to four consecutive
'a'
characters followed by a'b'
, capturing the consecutive'a'
s as variablea
. For more detailed usage, refer to lexer.mbt in moonbitlang/parser and the lexmatch proposal in moonbit-evolution.lexmatch x with longest { (("a{2,4}" as a) "b", _) => Some(a.length()) _ => None }
-
Unified Import Syntax with
using
Introduced the
using
syntax, which unifiesfnalias
,traitalias
, and simple typealias. When importing atype
ortrait
, the corresponding keyword must now be prefixed before the name.using @pkg { value, CONST, type Type, trait Trait, }
In addition,
pub using
can be used to achieve re-export, allowing definitions from other packages to be re-exported in the current package. In the future, thefnalias
,traitalias
, and simpletypealias
syntaxes will be deprecated β the functionality of creating aliases for external definitions will be replaced byusing
, while creating aliases for definitions within the current package will be handled by the#alias
attribute. -
Methods in a trait now support optional parameters.
pub(open) trait Reader { async read(Self, buf : FixedArray[Byte], offset? : Int, len? : Int) -> Unit }
The default value of an optional parameter is determined individually by each
impl
. Different implementations can define their own default values or choose not to provide one at all β in that case, the optional parameterβs type within theimpl
will beT?
, whereNone
indicates that the user did not supply the argument. -
Operator Overloading via
#alias
Added support for using
#alias
to overload operators such asop_get
, providing better readability compared to theop_xxx
naming convention.// Previously `op_get` #alias("_[_]") fn[X] Array::get(self : Array[X], index : Int) -> X { ... } // Previously `op_set` #alias("_[_]=_") fn[X] Array::set(self : Array[X], index : Int, elem : X) -> Unit { ... } // Previously `op_as_view` #alias("_[_:_]") fn[X] Array::view( self : Array[X], start? : Int = 0, end? : Int = self.length(), ) -> ArrayView[X] { ... }
Currently, the following operators are supported. The actual implementation names (e.g.,
get
,set
,view
) can be freely chosen β simply attach the corresponding#alias
to enable operator overloading. It is recommended to use#alias
instead ofop_xxx
for method-based operator overloading. (Note: Operators like+
are overloaded through traits and are not affected by this change.) -
Removal of Long-Deprecated Syntax and Behaviors
Several language features that had been deprecated for a long time have now been officially removed:
Previously, methods defined in the form
fn meth(self : T, ..)
were both methods and regular functions, allowing them to be called directly as standalone functions. This behavior had long been deprecated (with compiler warnings) and is now removed. The declarationfn meth(self : T, ..)
is now equivalent tofn T::meth(self : T, ..)
. In the future, theself
-form method definition itself may also be deprecated.The
direct_use
field in moon.pkg.json has been officially removed and replaced byusing
.
Toolchain Updatesβ
-
Wasm Toolchain Released β The Wasm version of the MoonBit toolchain is now available for x86 Darwin and ARM Linux users. π Read more
-
Experimental Build System (RR) β A new experimental version of the build system, codenamed
RR
, has been introduced. It offers higher performance and better maintainability, and will eventually replace the current internal implementation ofmoon
. You can enable it using the environment variableNEW_MOON=1
or the command-line flag-Z rupes_recta
. If you encounter any issues, please report them on GitHub Issues. -
moon fmt
Enhancements β Now supports formatting of.mbt.md
files. -
moon info --no-alias
Option β Added a new option to hide type aliases when generating thepkg.generated.mbti
file.
Standard Library Updatesβ
-
Improved Hash Security β To mitigate potential HashDoS attacks, hash computation is now randomized per process. This change has already been implemented in the JS backend.
-
Immutable
ArrayView
βArrayView
has been changed to an immutable data structure, providing a unified slicing interface forArray
,FixedArray
, andImmutArray
.