MoonBit Language Reference
@reference/index.md @reference/introduction.md @reference/fundamentals.md @reference/methods.md @reference/derive.md @reference/error-handling.md @reference/packages.md @reference/tests.md @reference/benchmarks.md @reference/docs.md @reference/attributes.md @reference/ffi.md @reference/async-experimental.md @reference/error_codes/index.md @reference/toml-parser-parser.mbt
Official Packages
MoonBit has official packages maintained by the team:
- moonbitlang/x: Utilities including file I/O (
moonbitlang/x/fs) - moonbitlang/async: Asynchronous runtime with TCP, HTTP, async queues, async test, and async main
To use these packages:
- Add the dependency:
moon add moonbitlang/xormoon add moonbitlang/async - Import the specific subpackage in
moon.pkg.json:json1{"import": ["moonbitlang/x/fs"]}
Common Pitfalls
- Use
suberrorfor error types,raiseto throw,try! func() |> ignoreto ignore errors - Use
func() |> ignorenotlet _ = func() - When using
inspect(value, content=expected_string), don't declare a separatelet expected = ...variable - it causes unused variable warnings. Put the expected string directly in thecontent=parameter - Use
!conditionnotnot(condition) - Use
f(value)notf!(value)(deprecated) - Use
for i in 0..<nnot C-stylefor i = 0; i < n; i = i + 1 - Use
if opt is Pattern(v) { ... }for single-branch matching, notmatch opt {} - Use
arr.clear()notwhile arr.length() > 0 { arr.pop() } - Use
s.code_unit_at(i)orfor c in snots[i](deprecated) - Struct/enum visibility:
priv(hidden) < (none)/abstract (type only) <pub(readonly) <pub(all)(full) - Default to abstract (no modifier) for internal types; use
pub structwhen external code reads fields - Use
pub(all) enumfor enums that external code pattern-matches on - Use
let mutonly for reassignment, not for mutable containers like Array - Use
reinterpret_as_uint()for unsigned ops,to_int()for numeric conversion - Use
Array::length()notArray::size() - In moon.pkg.json, use "import", "test-import" and "wbtest-import" to manage package importing for ".mbt", "_test.mbt" and "_wbtest.mbt"
- Use
Option::unwrap_ornotOption::or
Parser Style Reference
When writing a hand-written parser, follow the style in reference/toml-parser-parser.mbt (mirrored from moonbit-community/toml-parser).
- Prefer
Parser::parse_*methods that advance a token view (view/update_view) - Centralize error reporting (e.g.
Parser::error) and include token locations - Keep functions small (
parse_value,parse_array, ...) and separate blocks with///|