Loading...
Loading...
Guide for writing, refactoring, and testing MoonBit projects. Use when working in MoonBit modules or packages, organizing MoonBit files, using moon tooling (build/check/run/test/doc/ide etc.), or following MoonBit-specific layout, documentation, and testing conventions.
npx skill4agent add pnsk-lab/hikkaku moonbit-agent-guidemoon.mod.jsonmoon.pkgmoon.pkg.jsonmoon ide docmoon ide outlinemoon ide peek-defmoon ide find-referencesmoon ide rename--loc filename:line:col#deprecated#alias(old_api, deprecated)#deprecated#alias///|moon checkmoon test [dirname|filename] --filter 'glob'moon test --updatemoon fmtmoon infopkg.generated.mbtimoon ide outlinemoon ide peek-defmoon ide find-referencesmoon checkmoon test [dirname|filename] --filter 'glob'moon fmtmoon infopkg.generated.mbtimoon ide renamemoon ide find-referencesmoon ide peek-defmoon ide rename <symbol> <new_name> --loc filename:line:colmoon checkmoon test [dirname|filename]moon fmtmoon infomoon ide doc///|moon checkmoon test [dirname|filename]--updatemoon fmtmoon infopkg.generated.mbti.mbt.mbtimoon.mod.jsonmoon.pkgmoon.pkg.jsonmoon.mod.jsonmy_module
├── moon.mod.json # Module metadata, source field (optional) specifies the source directory of the module
├── moon.pkg # Package metadata (each directory is a package like Golang)
├── README.mbt.md # Markdown with tested code blocks (`test "..." { ... }`)
├── README.md -> README.mbt.md
├── cmd # Command line directory
│ └── main
│ ├── main.mbt
│ └── moon.pkg # executable package with `options("is-main": true)`
├── liba/ # Library packages
│ └── moon.pkg # Referenced by other packages as `@username/my_module/liba`
│ └── libb/ # Library packages
│ └── moon.pkg # Referenced by other packages as `@username/my_module/liba/libb`
├── user_pkg.mbt # Root packages, referenced by other packages as `@username/my_module`
├── user_pkg_wbtest.mbt # White-box tests (only needed for testing internal private members, similar to Golang's package mypackage)
└── user_pkg_test.mbt # Black-box tests
└── ... # More package files, symbols visible to current package (like Golang)moon.mod.jsonmoon.pkgmoon.pkg.jsonmoonmoon.mod.jsonnamemoon.mod.json.mbt///|*_test.mbt*_test.mbt*.mbt.mdmbt checkREADME.mbt.mdmbt checkREADME.mbt.mdREADME.mdpkg.generated.mbtipkg.generated.mbtimoon infopkg.generated.mbtipkg.generated.mbtimoon idemutmutmutreturnget()i = i + 1i += 1tryfunction_name!(...)function_name(...)?try?forfor i in 0..<(n-1) {...}for j in 0..=6 {...}awaitasync[pub] async fn ...async test ...moonmoon new my_projectmoon run cmd/mainmoon buildmoon runmoon build--targetmoon checkmoon check--targetmoon infombtimoon info--targetmoon check --target allmoon add packagemoon remove packagemoon fmtmoon -C dir checkmoon testmoon test--targetmoon test --updatemoon test -vmoon test [dirname|filename]moon coverage analyzemoon test [dirname|filename] --filter 'glob'moon test float/float_test.mbt --filter "Float::*"
moon test float -F "Float::*" // shortcut syntaxREADME.mbt.mdREADME.mbt.md*.mbt.mdmbt checkmbt checkmoon checkmoon testmbt nocheckmbt nocheckREADME.mbt.mdREADME.mdREADME.mdinspect(value, content="...")inspect(value)moon test --updatemoon test -uinspect()Show@json.inspect()ToJsoninspect@json.inspectimpl (Show|ToJson) for YourTypederive (Show, ToJson)moon test --updatecontent=Agent WorkflowFast Task Playbooks@package.fntest "..." { ... }test "panic ..." {...}ignore(...)try? f()Result[...]inspect///|
/// Get the largest element of a non-empty `Array`.
///
/// # Example
/// ```mbt check
/// test {
/// inspect(sum_array([1, 2, 3, 4, 5, 6]), content="21")
/// }
/// ```
///
/// # Panics
/// Panics if the `xs` is empty.
pub fn sum_array(xs : Array[Int]) -> Int {
xs.fold(init=0, (a, b) => a + b)
}moon test --updatembt checktestasync testspec.mbt///|
declare pub type Yaml
///|
declare pub fn Yaml::to_string(y : Yaml) -> String raise
///|
declare pub impl Eq for Yaml
///|
declare pub fn parse_yaml(s : String) -> Yaml raisespec_easy_test.mbtspec_difficult_test.mbtmoon checkdeclaremoon testdeclarepub type Yamlmoon ide [doc|peek-def|outline|find-references|hover|rename]moon ide doc <query>moon ide docgrep_searchmoon ide outline .moon ide find-references <symbol>moon ide peek-defmoon ide hover sym --loc filename:line:colmoon ide rename <symbol> <new_name> [--loc filename:line:col]--locgrepmoon ide docmoon ide docmoon ide doc ''moon ide doc "[@pkg.]value_or_function_name"moon ide doc "[@pkg.]Type_name"moon ide doc "[@pkg.]Type_name::method_or_field_name"moon ide doc "@pkg"pkgmoon ide doc "@json"@jsonmoon ide doc "@encoding/utf8"*moon ide doc "String::*rev*"moon ide doc# search for String methods in standard library:
$ moon ide doc "String"
type String
pub fn String::add(String, String) -> String
# ... more methods omitted ...
$ moon ide doc "@buffer" # list all symbols in package buffer:
moonbitlang/core/buffer
fn from_array(ArrayView[Byte]) -> Buffer
# ... omitted ...
$ moon ide doc "@buffer.new" # list the specific function in a package:
package "moonbitlang/core/buffer"
pub fn new(size_hint? : Int) -> Buffer
Creates ... omitted ...
$ moon ide doc "String::*rev*" # globbing
package "moonbitlang/core/string"
pub fn String::rev(String) -> String
Returns ... omitted ...
# ... more
pub fn String::rev_find(String, StringView) -> Int?
Returns ... omitted ...Agent Workflowmoon ide rename sym new_name [--loc filename:line:col]compute_sumcalculate_sum$ moon ide rename compute_sum calculate_sum --loc math_utils.mbt:2
*** Begin Patch
*** Update File: cmd/main/main.mbt
@@
///|
fn main {
- println(@math_utils.compute_sum(1, 2))
+ println(@math_utils.calculate_sum(1, 2))
}
*** Update File: math_utils.mbt
@@
///|
-pub fn compute_sum(a: Int, b: Int) -> Int {
+pub fn calculate_sum(a: Int, b: Int) -> Int {
a + b
}
*** Update File: math_utils_test.mbt
@@
///|
test {
- inspect(@math_utils.compute_sum(1, 2))
+ inspect(@math_utils.calculate_sum(1, 2))
}
*** End Patchmoon ide hover sym --loc filename:line:colfilter$ moon ide hover filter --loc hover.mbt:14
test {
let a: Array[Int] = [1]
inspect(a.filter((x) => {x > 1}))
^^^^^^
```moonbit
fn[T] Array::filter(self : Array[T], f : (T) -> Bool raise?) -> Array[T] raise?
```
---
Creates a new array containing all elements from the input array that satisfy
... omitted ...
}moon ide peek-def sym [--loc filename:line:col]Parser::read_u32_leb128moon ide peek-def Parser::read_u32_leb128grepL45:|///|
L46:|fn Parser::read_u32_leb128(self : Parser) -> UInt raise ParseError {
L47:| ...
...:| }Parser$ moon ide peek-def Parser --loc src/parse.mbt:46:4
Definition found at file src/parse.mbt
| ///|
2 | priv struct Parser {
| ^^^^^^
| bytes : Bytes
| mut pos : Int
| }
|--locParser$ moon ide peek-def String::rev
Found 1 symbols matching 'String::rev':
`pub fn String::rev` in package moonbitlang/core/builtin at /Users/usrname/.moon/lib/core/builtin/string_methods.mbt:1039-1044
1039 | ///|
| /// Returns a new string with the characters in reverse order. It respects
| /// Unicode characters and surrogate pairs but not grapheme clusters.
| pub fn String::rev(self : String) -> String {
| self[:].rev()
| }moon ide outline [dir|file]moon ide find-references <sym>moon ide outlinemoon ide outline dirmoon ide outline parser.mbtgoto-definitionmoon ide find-references TranslationUnit$ moon ide outline .
spec.mbt:
L003 | pub(all) enum CStandard {
...
L013 | pub(all) struct Position {
...$ moon ide find-references TranslationUnitmoon add moonbitlang/x # Add latest version
moon add moonbitlang/x@0.4.6 # Add specific versionmoon update # Update package indexmoon.mod.json{
"name": "username/hello", // Required format for published modules
"version": "0.1.0",
"source": ".", // Source directory(optional, default: ".")
"repository": "", // Git repository URL
"keywords": [], // Search keywords
"description": "...", // Module description
"deps": {
// Dependencies from mooncakes.io, using`moon add` to add dependencies
"moonbitlang/x": "0.4.6"
}
}moon.pkgimport {
"username/hello/liba",
"moonbitlang/x/encoding" @libb
}
import {...} for "test"
import {...} for "wbtest"
options("is-main" : true) // other options{
"is_main": true, // Creates executable when true
"import": [ // Package dependencies
"username/hello/liba", // Simple import, use @liba.foo() to call functions
{
"path": "moonbitlang/x/encoding",
"alias": "libb" // Custom alias, use @libb.encode() to call functions
}
],
"test-import": [...], // Imports for black-box tests, similar to import
"wbtest-import": [...] // Imports for white-box tests, similar to import (rarely used)
}moon.pkgmoon.pkg.json"module_name/package_path"@alias.function()libausername/hello/liba@packagename"username/hello/liba"@liba.function()import { "moonbitlang/x/encoding" @enc}@enc.function()_test.mbt_wbtest.mbt///|
/// In main.mbt after importing "username/hello/liba" in `moon.pkg`
fn main {
println(@liba.hello()) // Calls hello() from liba package
}moonbitlang/core/strconvmoon.pkgfib../fib/./fib/moon.pkg.mbt import {
"username/hello/fib",
}conditional compilationlink configurationwarning controlpre-build commandsreferences/advanced-moonbit-build.mdifmatchRef[T]///|fnpubpub(all)import@alias.fnmoon.pkg...let mutErrorsuberrorraisetry?Result[...]try { } catch { }try!///|
/// Declare error types with 'suberror'
suberror ValueError {
ValueError(String)
}
///|
/// Tuple struct to hold position info
struct Position(Int, Int) derive(ToJson, Show, Eq)
///|
/// ParseError is subtype of Error
pub(all) suberror ParseError {
InvalidChar(pos~ : Position, Char) // pos is labeled
InvalidEof(pos~ : Position)
InvalidNumber(pos~ : Position, String)
InvalidIdentEscape(pos~ : Position)
} derive(Eq, ToJson, Show)
///|
/// Functions declare what they can throw
fn parse_int(s : String, position~ : Position) -> Int raise ParseError {
// 'raise' throws an error
if s is "" {
raise ParseError::InvalidEof(pos=position)
}
... // parsing logic
}
///|
/// Just declare `raise` to not track specific error types
fn div(x : Int, y : Int) -> Int raise {
if y is 0 {
fail("Division by zero")
}
x / y
}
///|
test "inspect raise function" {
let result : Result[Int, Error] = try? div(1, 0)
guard result is Err(Failure(msg)) && msg.contains("Division by zero") else {
fail("Expected error")
}
}
// Three ways to handle errors:
///|
/// Propagate automatically
fn use_parse(position~ : Position) -> Int raise ParseError {
let x = parse_int("123", position~) // label punning, equivalent to position=position
// Error auto-propagates by default.
// Unlike Swift, you do not need to mark `try` for functions that can raise
// errors; the compiler infers it automatically. This keeps error handling
// explicit but concise.
x * 2
}
///|
/// Mark `raise` for all possible errors, do not care which error it is.
/// For quick prototypes, `raise` is acceptable.
fn use_parse2(position~ : Position) -> Int raise {
let x = parse_int("123", position~) // label punning
x * 2
}
///|
/// Convert to Result with try?
fn safe_parse(s : String, position~ : Position) -> Result[Int, ParseError] {
let val1 : Result[_] = try? parse_int(s, position~) // Returns Result[Int, ParseError]
// try! is rarely used - it panics on error, similar to unwrap() in Rust
// let val2 : Int = try! parse_int(s) // Returns Int otherwise crash
// Alternative explicit handling:
let val3 = try parse_int(s, position~) catch {
err => Err(err)
} noraise { // noraise block is optional - handles the success case
v => Ok(v)
}
...
}
///|
/// Handle with try-catch
fn handle_parse(s : String, position~ : Position) -> Int {
try parse_int(s, position~) catch {
ParseError::InvalidEof => {
println("Parse failed: InvalidEof")
-1 // Default value
}
_ => 2
}
}asyncByteInt16IntUInt16UIntInt64UInt64///|
test "integer and char literal overloading disambiguation via type in the current context" {
let a0 = 1 // a is Int by default
let (int, uint, uint16, int64, byte) : (Int, UInt, UInt16, Int64, Byte) = (
1, 1, 1, 1, 1,
)
assert_eq(int, uint16.to_int())
let a1 : Int = 'b' // this also works, a5 will be the unicode value
let a2 : Char = 'b'
}///|
test "bytes literals overloading and indexing" {
let b0 : Bytes = b"abcd"
let b1 : Bytes = "abcd" // b" prefix is optional, when we know the type
let b2 : Bytes = [0xff, 0x00, 0x01] // Array literal overloading
guard b0 is [b'a', ..] && b0[1] is b'b' else {
// Bytes can be pattern matched as BytesView and indexed
fail("unexpected bytes content")
}
}///|
test "array literals overloading: disambiguation via type in the current context" {
let a0 : Array[Int] = [1, 2, 3] // resizable
let a1 : FixedArray[Int] = [1, 2, 3] // Fixed size
let a2 : ReadOnlyArray[Int] = [1, 2, 3]
let a3 : ArrayView[Int] = [1, 2, 3]
}s[i]s.get_char(i)Char?///|
test "string indexing and utf8 encode/decode" {
let s = "hello world"
let b0 : UInt16 = s[0]
guard b0 is ('\n' | 'h' | 'b' | 'a'..='z') && s is [.. "hello", .. rest] else {
fail("unexpected string content")
}
guard rest is " world" // otherwise will crash (guard without else)
// In check mode (expression with explicit type), ('\n' : UInt16) is valid.
// Using get_char for Option handling
let b1 : Char? = s.get_char(0)
assert_true(b1 is Some('a'..='z'))
// ⚠️ Important: Variables won't work with direct indexing
let eq_char : Char = '='
// s[0] == eq_char // ❌ Won't compile - eq_char is not a literal, lhs is UInt while rhs is Char
// Use: s[0] == '=' or s.get_char(0) == Some(eq_char)
let bytes = @utf8.encode("中文") // utf8 encode package is in stdlib
assert_true(bytes is [0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87])
let s2 : String = @utf8.decode(bytes) // decode utf8 bytes back to String
assert_true(s2 is "中文")
for c in "中文" {
let _ : Char = c // unicode safe iteration
println("char: \{c}") // iterate over chars
}
}\{}Show///|
test "string interpolation basics" {
let name : String = "Moon"
let config = { "cache": 123 }
let version = 1.0
println("Hello \{name} v\{version}") // "Hello Moon v1.0"
// ❌ Wrong - quotes inside interpolation not allowed:
// println(" - Checking if 'cache' section exists: \{config["cache"]}")
// ✅ Correct - extract to variable first:
let has_key = config["cache"] // `"` not allowed in interpolation
println(" - Checking if 'cache' section exists: \{has_key}")
let sb = StringBuilder::new()
sb
..write_char('[') // dotdot for imperative method chaining
..write_view([1, 2, 3].map(x => "\{x}").join(","))
..write_char(']')
inspect(sb.to_string(), content="[1,2,3]")
}\{}///|
test "multi-line string literals" {
let multi_line_string : String =
#|Hello "world"
#|World
#|
let multi_line_string_with_interp : String =
$|Line 1 ""
$|Line 2 \{1+2}
$|
// no escape in `#|`,
// only escape '\{..}` in `$|`
assert_eq(multi_line_string, "Hello \"world\"\nWorld\n")
assert_eq(multi_line_string_with_interp, "Line 1 \"\"\nLine 2 3\n")
}///|
test "map literals and common operations" {
// Map literal syntax
let map : Map[String, Int] = { "a": 1, "b": 2, "c": 3 }
let empty : Map[String, Int] = {} // Empty map, preferred
let also_empty : Map[String, Int] = Map::new()
// From array of pairs
let from_pairs : Map[String, Int] = Map::from_array([("x", 1), ("y", 2)])
// Set/update value
map["new-key"] = 3
map["a"] = 10 // Updates existing key
// Get value - returns Option[T]
guard map is { "new-key": 3, "missing"? : None, .. } else {
fail("unexpected map contents")
}
// Direct access (panics if key missing)
let value : Int = map["a"] // value = 10
// Iteration preserves insertion order
for k, v in map {
println("\{k}: \{v}") // Prints: a: 10, b: 2, c: 3, new-key: 3
}
// Other common operations
map.remove("b")
guard map is { "a": 10, "c": 3, "new-key": 3, .. } && map.length() == 3 else {
// "b" is gone, only 3 elements left
fail("unexpected map contents after removal")
}
}StringViewBytesViewArrayView[T][:]StringBytesArray*ViewStringStringViews[:]s[start:end]s[start:]s[:end]BytesBytesViewb[:]b[start:end]Array[T]FixedArray[T]ReadOnlyArray[T] → viaors[a:b]try! s[a:b][first, .. rest].to_string().to_bytes().to_array()moon ide doc StringViewenumstruct///|
enum Tree[T] {
Leaf(T) // Unlike Rust, no comma here
Node(left~ : Tree[T], T, right~ : Tree[T]) // enum can use labels
} derive(Show, ToJson) // derive traits for Tree
///|
pub fn Tree::sum(tree : Tree[Int]) -> Int {
match tree {
Leaf(x) => x
// we don't need to write Tree::Leaf, when `tree` has a known type
Node(left~, x, right~) => left.sum() + x + right.sum() // method invoked in dot notation
}
}
///|
struct Point {
x : Int
y : Int
} derive(Show, ToJson) // derive traits for Point
///|
test "user defined types: enum and struct" {
@json.inspect(Point::{ x: 10, y: 20 }, content={ "x": 10, "y": 20 })
}for///|
pub fn binary_search(arr : ArrayView[Int], value : Int) -> Result[Int, Int] {
let len = arr.length()
// functional for loop:
// initial state ; [predicate] ; [post-update] {
// loop body with `continue` to update state
//} else { // exit block
// }
// predicate and post-update are optional
for i = 0, j = len; i < j; {
// post-update is omitted, we use `continue` to update state
let h = i + (j - i) / 2
if arr[h] < value {
continue h + 1, j // functional update of loop state
} else {
continue i, h // functional update of loop state
}
} else { // exit of for loop
if i < len && arr[i] == value {
Ok(i)
} else {
Err(i)
}
} where {
invariant: 0 <= i && i <= j && j <= len,
invariant: i == 0 || arr[i - 1] < value,
invariant: j == len || arr[j] >= value,
reasoning: (
#|For a sorted array, the boundary invariants are witnesses:
#| - `arr[i-1] < value` implies all arr[0..i) < value (by sortedness)
#| - `arr[j] >= value` implies all arr[j..len) >= value (by sortedness)
#|
#|Preservation proof:
#| - When arr[h] < value: new_i = h+1, and arr[new_i - 1] = arr[h] < value ✓
#| - When arr[h] >= value: new_j = h, and arr[new_j] = arr[h] >= value ✓
#|
#|Termination: j - i decreases each iteration (h is strictly between i and j)
#|
#|Correctness at exit (i == j):
#| - By invariants: arr[0..i) < value and arr[i..len) >= value
#| - So if value exists, it can only be at index i
#| - If arr[i] != value, then value is absent and i is the insertion point
#|
),
}
}
///|
test "functional for loop control flow" {
let arr : Array[Int] = [1, 3, 5, 7, 9]
inspect(binary_search(arr, 5), content="Ok(2)") // Array to ArrayView implicit conversion when passing as arguments
inspect(binary_search(arr, 6), content="Err(3)")
// for iteration is supported too
for i, v in arr {
println("\{i}: \{v}") // `i` is index, `v` is value
}
}forwherewhereforfor .. infor ... {
...
} where {
invariant : <boolean_expr>, // checked at runtime in debug builds
invariant : <boolean_expr>, // multiple invariants allowed
reasoning : <string> // documentation for proof sketch
}arr[i-1] < valuearr[0..i) < value||i == 0 || arr[i-1] < valuecontinue///|
fn g(
positional : Int,
required~ : Int,
optional? : Int, // no default => Option
optional_with_default? : Int = 42, // default => plain Int
) -> String {
// These are the inferred types inside the function body.
let _ : Int = positional
let _ : Int = required
let _ : Int? = optional
let _ : Int = optional_with_default
"\{positional},\{required},\{optional},\{optional_with_default}"
}
///|
test {
inspect(g(1, required=2), content="1,2,None,42")
inspect(g(1, required=2, optional=3), content="1,2,Some(3),42")
inspect(g(1, required=4, optional_with_default=100), content="1,4,None,100")
}arg : Type?NoneSome(...)///|
fn with_config(a : Int?, b : Int?, c : Int) -> String {
"\{a},\{b},\{c}"
}
///|
test {
inspect(with_config(None, None, 1), content="None,None,1")
inspect(with_config(Some(5), Some(5), 1), content="Some(5),Some(5),1")
}arg? : Type?b? : Int = 1b? : Int? = Some(1)///|
fn f_misuse(a? : Int?, b? : Int = 1) -> Unit {
let _ : Int?? = a // rarely intended
let _ : Int = b
}
// How to fix: declare `(a? : Int, b? : Int = 1)` directly.
///|
fn f_correct(a? : Int, b? : Int = 1) -> Unit {
let _ : Int? = a
let _ : Int = b
}
///|
test {
f_misuse(b=3)
f_misuse(a=Some(5), b=2) // works but confusing
f_correct(b=2)
f_correct(a=5)
}arg : APIOptions///|
/// Do not use struct to group options.
struct APIOptions {
width : Int?
height : Int?
}
///|
fn not_idiomatic(opts : APIOptions, arg : Int) -> Unit {
}
///|
test {
// Hard to use in call site
not_idiomatic({ width: Some(5), height: None }, 10)
not_idiomatic({ width: None, height: None }, 10)
}references/moonbit-language-fundamentals.mbt.md