2025-02-10
Language Updatesβ
New is Expressionβ
- The syntax for this expression is
expr is pat. The expression is aBooltype and evaluates totruewhenexprmatches the patternpat. For example:
fn use_is_expr(x: Int?) -> Unit {
if x is Some(i) && i >= 10 { ... }
}
- The pattern can introduce new binders, which can be used in the following cases:
- In
e1 && e2, ife1is anisexpression, the binders introduced by the pattern can be used ine2. - In
if e1 && e2 && ... { if_branch } else { ... }, binders introduced byisexpressions in the&&-chained conditions likee1&e2can be used in theif_branch.
String Construction and Pattern Matchingβ
- New support for constructing strings using array spread syntax, for example:
fn string_spread() -> Unit {
let s = "helloπ€£ππ"
let sv = s[1:6]
let str : String = ['x', ..s, ..sv, 'π']
println(str) // xhelloπ€£ππelloπ€£π
}
In an array spread, individual elements are Char values. You can use .. to insert a String or a @string.View segment. This syntax is equivalent to using StringBuilder to construct a string.
- Support for pattern matching on strings using array patterns, which can be mixed with array literal patterns, for example:
fn match_str(s: String) -> Unit {
match s {
"hello" => ... // string literal pattern
[ 'a' ..= 'z', .. ] => ... // array pattern
[ .., 'π' ] => ... // array pattern with unicode
_ => ...
}
}
New Compiler Warningsβ
- The compiler now warns about unused
guardstatements and missing cases inguard let ... else ....
fn main {
guard let (a, b) = (1, 2)
^^^^^^^^^ ----- useless guard let
println(a + b)
}
moonfmt Fixesβ
- Fixed formatting errors related to
asynccode inmoonfmt. - Adjusted insertion rules for
///|markers.
Package Updatesβ
-
moonbitlang/x/sysnow supports the native backend and fixes inconsistencies across different operating systems. -
The
fspackage inmoonbitlang/xhas been updated with improved error handling. -
String-related operations are being reorganized. The
stringpackage will provide more Unicode-safe APIs while deprecating some APIs that expose UTF-16 implementation details. During this transition,stringmethods may become unstable. It is recommended to use iter methods or pattern matching to access string elements. -
Refactored
ArrayView/StringView/BytesViewtypes by moving them from the@builtinpackage to their respective type-related packages. Their names have been updated accordingly to@array.View/@string.View/@bytes.View.
IDE Updatesβ
-
Added code action support for filling in missing cases in pattern matching.
-
Enabled inline autocompletion for all cases in empty pattern matches.
-
Fixed a bug in trait method "Go to Reference".
-
Fixed missing autocompletion for variables introduced in
guard let ... else ...and improved pattern completion inelsebranches.
Build System Updatesβ
- Fixed a bug in
moon testwhere panic tests were being skipped on the native backend.
Documentation Updatesβ
-
Added an Error Code Index to the MoonBit documentation. Many entries are still incomplete, and we welcome contributions! You can contribute error codes by referring to this issue.
-
MoonBit Language Tour now supports Chinese.