Help us improve
Share bugs, ideas, or general feedback.
From moonbit
MoonBit language reference covering syntax, types, functions, methods, and deriving. Use when writing MoonBit code, debugging MoonBit programs, or answering questions about MoonBit syntax and features.
npx claudepluginhub totto2727-org/monorepoHow this skill is triggered — by the user, by Claude, or both
Slash command
/moonbit:moonbit-docsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
<!-- Derived from MoonBit documentation by moonbitlang -->
references/language-async-experimental.mdreferences/language-attributes.mdreferences/language-benchmarks.mdreferences/language-derive.mdreferences/language-docs.mdreferences/language-error-codes-index.mdreferences/language-error-handling.mdreferences/language-ffi.mdreferences/language-fundamentals-built-in-data-structures.mdreferences/language-fundamentals-control-structures.mdreferences/language-fundamentals-custom-data-types.mdreferences/language-fundamentals-functions.mdreferences/language-fundamentals-generics.mdreferences/language-fundamentals-iterator.mdreferences/language-fundamentals-overloaded-literals.mdreferences/language-fundamentals-pattern-matching.mdreferences/language-fundamentals-special-syntax.mdreferences/language-methods.mdreferences/language-packages.mdreferences/language-tests.mdProvides behavioral guidelines to reduce common LLM coding mistakes, focusing on simplicity, surgical changes, assumption surfacing, and verifiable success criteria.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Fetches up-to-date documentation from Context7 for libraries and frameworks like React, Next.js, Prisma. Use for setup questions, API references, and code examples.
Share bugs, ideas, or general feedback.
A MoonBit program consists of top-level definitions including:
init functions, main function and/or test blocks.MoonBit distinguishes between statements and expressions. In a function body, only the last clause should be an expression, which serves as a return value. For example:
fn foo() -> Int {
let x = 1
x + 1
}
fn bar() -> Int {
let x = 1
//! x + 1
x + 2
}
Expressions include:
a[0]), struct fields (e.g r.x), tuple components (e.g. t.0), etc.match, if, loop expressions, etc.Statements include:
return statementsUnit, (e.g. ignore)A code block can contain multiple statements and one expression, and the value of the expression is the value of the code block.
A variable can be declared as mutable or immutable using let mut or let, respectively. A mutable variable can be reassigned to a new value, while an immutable one cannot.
A constant can only be declared at top level and cannot be changed.
let zero = 0
const ZERO = 0
fn main {
//! const ZERO = 0
let mut i = 10
i = 20
println(i + zero + ZERO)
}
A top level variable binding
Ref instead)Variables, functions should start with lowercase letters a-z and can contain letters, numbers, underscore, and other non-ascii unicode chars.
It is recommended to name them with snake_case.
Constants, types should start with uppercase letters A-Z and can contain letters, numbers, underscore, and other non-ascii unicode chars.
It is recommended to name them with PascalCase or SCREAMING_SNAKE_CASE.
The following are the keywords and should not be used:
[
"as",
"else",
"extern",
"fn",
"fnalias",
"if",
"let",
"const",
"match",
"using",
"mut",
"type",
"typealias",
"struct",
"enum",
"trait",
"traitalias",
"derive",
"while",
"break",
"continue",
"import",
"return",
"throw",
"raise",
"try",
"catch",
"pub",
"priv",
"readonly",
"true",
"false",
"_",
"test",
"loop",
"for",
"in",
"impl",
"with",
"guard",
"async",
"is",
"suberror",
"and",
"letrec",
"enumview",
"noraise",
"defer"
]
The following are the reserved keywords. Using them would introduce a warning. They might be turned into keywords in the future.
[
"module",
"move",
"ref",
"static",
"super",
"unsafe",
"use",
"where",
"await",
"dyn",
"abstract",
"do",
"final",
"macro",
"override",
"typeof",
"virtual",
"yield",
"local",
"method",
"alias",
"assert",
"package",
"recur",
"using",
"enumview",
"isnot",
"define",
"downcast",
"inherit",
"member",
"namespace",
"static",
"upcast",
"use",
"void",
"lazy",
"include",
"mixin",
"protected",
"sealed",
"constructor",
"atomic",
"volatile",
"anyframe",
"anytype",
"asm",
"await",
"comptime",
"errdefer",
"export",
"opaque",
"orelse",
"resume",
"threadlocal",
"unreachable",
"dynclass",
"dynobj",
"dynrec",
"var",
"finally",
"noasync"
]
init and mainThere is a specialized function called init function. The init function is special:
init functions in the same package.init function can't be explicitly called or referred to by other functions.
Instead, all init functions will be implicitly called when initializing a package. Therefore, init functions should only consist of statements.fn init {
let x = 1
println(x)
}
There is another specialized function called main function. The main function is the main entrance of the program, and it will be executed after the initialization stage.
Same as the init function, it has no parameter list nor return type.
fn main {
let x = 2
println(x)
}
The previous two code snippets will print the following at runtime:
1
2
Only packages that are main packages can define such main function. Check out build system tutorial for detail. In current projects, this is configured in moon.pkg:
options(
"is-main": true,
)
testThere's also a top-level structure called test block. A test block defines inline tests, such as:
test "test_name" {
assert_eq(1 + 1, 2)
assert_eq(2 + 2, 4)
inspect([1, 2, 3], content="[1, 2, 3]")
}
The following contents will use test block and main function to demonstrate the execution result,
and we assume that all the test blocks pass unless stated otherwise.