2024-12-02
Language Updates
- Added support for range patterns to match a range of integer or character values in pattern matching.
The syntax for range patterns is a..<b
(excluding the upper bound b
) or a..=b
(including the upper bound b
). Bounds can be literals, constants declared with const
, or _
, which indicates no constraint on that side:
const Zero = 0
fn sign(x : Int) -> Int {
match x {
_..<Zero => -1
Zero => 0
Zero..<_ => 1
}
}
fn classify_char(c : Char) -> String {
match c {
'a'..='z' => "lowercase"
'A'..='Z' => "uppercase"
'0'..='9' => "digit"
_ => "other"
}
}
- Support calling
trait
withx.f(...)
trait I {
f(Self) -> Unit
}
type MyType Int
impl I for MyType with f(self) { println(self._) }
fn main {
let my : MyType = 42
my.f()// Output 42
}
If x
has type T
, the x.f(...)
syntax resolves as follows:
- If
T
defines a methodf
, callT::f
. - If a
trait
doesimpl SomeTrait for T with f
in the same package asT
, callSomeTrait::f
. If multiplef
are found, the compiler reports an ambiguity error. - If neither of the above applies, the compiler searches for
impl SomeTrait for T with f
within the current package. Notice that this rule only work in current package. IfT
is defined outside the current package, local implementation forT
cannot be called with dot syntax outside.
These rules enhance the flexibility of MoonBit’s dot syntax while maintaining semantic clarity and strong refactor safety. Resolution of x.f(...)
only involves the package defining T
and the current package, avoiding issues caused by newly introduced dependencies.
- Added support for trait alias
No new syntax is introduced. Trait aliases use the same syntax as type aliases:
typealias MyShow = Show
impl MyShow for MyType with ...
IDE Updates
-
Web IDE now supports updating inspect tests.
-
Generated code can be revised in MoonBit AI before inserting, and the revised code is automatically checked for syntax and type correctness.
-
Fixed syntax highlighting issues for Markdown code blocks in documentation comments.
Build System Updates
-
moon check
now supports passing a warn list. -
moon test
can run tests embedded in project documentation.- Usage:
moon test --doc
runs all tests within documentation comments of the current project. - Note: Tests must be enclosed between lines with ```. For example:
- Usage:
-
Fixed an issue with local functions and return expressions in
moon fmt
. -
Fixed an issue in
moon fmt
which comments near array indexing syntax are misplaced during formatting. -
moon fmt
enabled-block-style
by default.
Docs Update
- MoonBit Docs is now available on readthedocs.io.