MoonBit code generation best practices. Use when writing MoonBit code to avoid common AI mistakes with syntax, tests, and benchmarks.
View on GitHubkazuph/dotfiles
moonbit-practice
January 25, 2026
Select agents to install to:
npx add-skill https://github.com/kazuph/dotfiles/blob/main/plugins/moonbit-practice/skills/practice/SKILL.md -a claude-code --skill moonbit-practiceInstallation paths:
.claude/skills/moonbit-practice/# MoonBit Practice Guide
Best practices for AI when generating MoonBit code.
If you don't understand something here, use `moonbit-docs` skill to search the official documentation.
## Guidelines
### Code Navigation: Prefer moon ide over Read/Grep
In MoonBit projects, **prefer `moon ide` commands over Read tool or grep**.
```bash
# ❌ Avoid: Reading files directly
Read src/parser.mbt
# ✅ Recommended: Find definitions from symbols
moon ide peek-def Parser::parse
moon ide goto-definition -tags 'pub fn' -query 'parse'
# ❌ Avoid: Searching with grep
grep -r "fn parse" .
# ✅ Recommended: Semantic search
moon ide find-references parse
moon ide outline src/parser.mbt
```
**Why:**
- `moon ide` provides semantic search (distinguishes definitions from call sites)
- grep picks up comments and strings
- `moon doc` quickly reveals APIs
### Other Rules
- **Prefer `moon.pkg` over `moon.pkg.json`** - Use `NEW_MOON_PKG=1 moon fmt` to convert
- Use `moon doc '<Type>'` to explore APIs before implementing
- Check reference/configuration.md before editing moon.pkg / moon.mod.json
- Check reference/agents.md when updating CLAUDE.md
## Common Pitfalls
- **Don't use uppercase for variables/functions** - compilation error
- **`mut` is only for reassignment, not field mutation** - Array push doesn't need it
- **`return` is unnecessary** - last expression is the return value
- **Methods require `Type::` prefix**
- **`++` `--` not supported** - use `i = i + 1` or `i += 1`
- **No `try` needed for error propagation** - automatic (unlike Swift)
- **No `await` keyword** - just declare with `async fn`
- **Prefer range for over C-style** - `for i in 0..<n {...}`
- **Legacy syntax**: `function_name!(...)` and `function_name(...)?` are deprecated
## Common Syntax Mistakes by AI
### Type Parameter Position
```moonbit
///| NG: fn identity[T] is old syntax
fn identity[T](val: T) -> T { val }
///| OK: Type parameter comes right after fn
fn[T] identity(val: T) -> T { val }
```
### raise SyntIssues Found: