2025-04-21
Language Updates
-
Async function syntax: The call syntax for async functions has changed to match the
errorstyle:f!(...). The oldf!!(...)syntax will now trigger a warning. -
Operator overloading migration to traits: Operator overloading is moving from method-based to
trait-based. Going forward, to overload an operator, you mustimplthe correspondingtraitfrom@moonbitlang/core/builtin. You can find the list of operator-related traits in the language docs andoperators.mbt.Migration details:
-
Old method-based operator overloading still works, but will raise a compiler warning.
-
Migrate by replacing
op_xxxmethods with the appropriateimplof the trait.- Operator traits enforce stricter type signatures—for example, the
-operator requires the return type to be the same as the input type. If an operator doesn’t match the expected signature, it must be rewritten as a regular method instead of using operator syntax.
- Operator traits enforce stricter type signatures—for example, the
-
If a trait defines
op_xxxmethods, they can be removed and replaced by adding the operator trait to thesuper traitlist. For example:// Old version trait Number { from_int(Int) -> Self op_add(Self, Self) -> Self op_sub(Self, Self) -> Self } // Migrated version trait Number : Add + Sub { from_int(Int) -> Self }
-
-
New
= _syntax for traits: You can now mark whether a method in atraithas a default implementation using= _:trait Hash { hash_combine(Self, Hasher) -> Unit hash(Self) -> Int = _ // indicates that `hash` has a default implementation }a. If a method is marked with
= _, a matching defaultimpl Trait with some_method(..)must exist. If a method has a defaultimplbut is not marked with= _, the compiler will warn you.b. This is mainly for readability: now you can see from the
traitdefinition which methods have defaults. Why not include the default code directly in thetrait? To keep the trait definition short and focused on method signatures. -
Implicit conversion from
Stringto@string.Viewis now supported. Also, slicing with[:]now returns a full view again. General slicing like[i:j]is still under design.fn f(v : @string.View) -> Unit { ignore(v) } fn main { f("hello") f("world"[:]) } -
Pattern matching on
@string.View/@bytes.Viewnow supports directly matching against string/byte literals:test { let s = "String" inspect!(s.view(end_offset=3) is "Str", content="true") let s : Bytes = "String" inspect!(s[:3] is "Str", content="true") } -
[Breaking Change] Updates to the
@stringcore package:- Many APIs now use
@string.Viewinstead ofStringfor parameters. - Some return types also changed from
StringtoView.
Notable changes:
Old Signature New Signature self.replace(old~: String, new~: String) -> Stringself.replace(old~: View, new~: View) -> Stringself.trim(charset: String) -> Stringself.trim(charset: View) -> Viewself.split(substr: String) -> Iter[String]self.split(substr: View) -> Iter[View]self.index_of(substr: String, from~: Int) -> Intself.find(substr: View) -> Option[Int]self.last_index_of(substr: String, from~: Int) -> Intself.rev_find(substr: View) -> Option[Int]self.starts_with(substr: String) -> Boolself.has_prefix(substr: View) -> Boolself.ends_with(substr: String) -> Boolself.has_suffix(substr: View) -> Bool - Many APIs now use
-
Core
Jsontype will become read-only: In the future, enum constructors forJsonwill no longer be available. Instead, helper functions are provided as replacements:test { let num = Json::number(3.14) let str = Json::string("Hello") let obj = Json::object({ "hello": num, "world": str }) }
Toolchain Updates
-
IDE drops support for
moonbit: truefront matter in Markdown:- Now, only files with the
.mbt.mdextension will be treated as MoonBit Markdown files in the IDE.
- Now, only files with the
-
IDE supports setting debug breakpoints in
.mbt.mddirectly:- You no longer need to enable
Debug: Allow Breakpoint Everywherein VSCode settings.
- You no longer need to enable
-
New
scriptsfield inmoon.mod.jsonfor build scripts:- Currently supports a
postaddscript: if a module contains apostaddfield, it will be automatically executed aftermoon add.- To skip execution, set the
MOON_IGNORE_POSTADDenvironment variable.
- To skip execution, set the
Example:
{ "scripts": { "postadd": "python3 build.py" } } - Currently supports a
-
Improvements to
.mbt.mdMarkdown support in themoonCLI:moon checknow includes Markdown checking automatically.moon testnow includes Markdown tests by default. (The--mdflag will be removed in the future.)