2025-01-13
Language Updates
-
Experimental Async Support
Experimental support for asynchronous programming has been added. You can declare asynchronous functions using
async fn ...
and call them withf!!(...)
. MoonBit also provides primitives for interrupting control flow. For details, see docs/async-experimental.Currently, the async standard library and event loop are under development. Using the JavaScript backend’s event loop and Promise API is easier for asynchronous programming. As this feature is experimental, breaking changes may occur based on feedback. We welcome and appreciate your testing and feedback.
-
Upcoming Changes to Method Semantics
Later this week, we will make major changes to simplify method-related rules. Currently:
-
Methods can be declared as
fn f(self: T, ..)
orfn T::f(..)
. -
Methods with
Self
as the first parameter can be called withxx.f(..)
. -
If unambiguous, methods can also be called with
f(..)
.
However, the last rule lacks user control and consistency between call syntax (
f(..)
) and declaration syntax (T::f(..)
). The new design will:-
Allow
fn f(self: T, ..)
to define methods callable withxx.f(..)
orf(..)
. These methods share the same namespace as regular functions and cannot have duplicate names. -
Allow
fn T::f(..)
to define methods callable withxx.f(..)
orT::f(..)
. These methods cannot be called as regular functions (f(..)
).
Intuition: All
fn f(..)
definitions are regular functions, whilefn T::f(..)
definitions are placed in a small namespace associated withT
.For methods like
new
(withoutSelf
as the first parameter), usefn new(..)
to enable direct calls asnew(..)
. Library authors are encouraged to:-
Use
fn f(..)
for unambiguous functions and make the first parameterself
forxx.f(..)
calls. -
Use
fn T::f(..)
for potentially ambiguous functions to place them in a scoped namespace.
-
-
Enhanced Alerts
-
Trigger alerts on type usage.
-
Trigger alerts on specific constructor usage.
-
-
Improvements to
ArrayView
/BytesView
/StringView
:
- Support for negative indices in view operations, e.g.:
let arr = [1, 2, 3, 4, 5]
let arr_view = arr[-4:-1]
println(arr_view) // [2, 3, 4]
- Support for pattern matching in
BytesView
with byte literals, e.g.:
fn f(bs: BytesView) -> Option[(Byte, BytesView)] {
match bs {
[b'a'..=b'z' as b, ..bs] => Some((b, bs)),
_ => None
}
}
let s = b"hello"[:]
let r = f(s)
println(r) // Some((b'\x68', b"\x65\x6c\x6c\x6f"))
- The
as
keyword in array patterns is now optional.[a, ..rest, b]
is valid, and the formatter will automatically omitas
from[a, ..as rest, b]
.
IDE Updates
- Added a command to toggle multi-line strings.
- Added more detailed information to workspace symbols, making it easier to search for specific functions and types.
Build System Updates
- Fixed a bug in doc tests affecting multi-line string test results.
Documentation Updates
-
MoonBit Tour now supports debug codelens with value tracking enabled by default.
-
Launch Moonbit Online Judge, a platform for users to learn MoonBit by solving problems.
Standard Library Updates
- Updated the behavior of
Int64
toJSON
conversions. Precision is no longer lost throughDouble
conversion; values are now preserved as strings.