2025-02-10
Language Updates
New is
Expression
- The syntax for this expression is
expr is pat
. The expression is aBool
type and evaluates totrue
whenexpr
matches 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
, ife1
is anis
expression, the binders introduced by the pattern can be used ine2
. - In
if e1 && e2 && ... { if_branch } else { ... }
, binders introduced byis
expressions in the&&
-chained conditions likee1
&e2
can 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
guard
statements 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
async
code inmoonfmt
. - Adjusted insertion rules for
///|
markers.
Package Updates
-
moonbitlang/x/sys
now supports the native backend and fixes inconsistencies across different operating systems. -
The
fs
package inmoonbitlang/x
has been updated with improved error handling. -
String-related operations are being reorganized. The
string
package will provide more Unicode-safe APIs while deprecating some APIs that expose UTF-16 implementation details. During this transition,string
methods may become unstable. It is recommended to use iter methods or pattern matching to access string elements. -
Refactored
ArrayView/StringView/BytesView
types by moving them from the@builtin
package 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 inelse
branches.
Build System Updates
- Fixed a bug in
moon test
where 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.