File contents 编写/改造 MoonBit 代码
触发条件
用户需要编写或修改 MoonBit 源代码时激活。
决策树
代码任务类型?
├── 定义数据类型
│ ├── 简单数据 → struct { fields }
│ ├── 带变体 → enum { A(...) B(...) }
│ └── 需外部构造 → pub(all) struct/enum
├── 需要泛型
│ ├── 函数级 → fn[T] func_name(...)
│ └── 类型级 → struct[T] Name { ... }
├── 需要多态行为
│ ├── 接口抽象 → pub(open) trait { fn method(...) }
│ └── 类型枚举 → match expr { :TypeA => ... }
├── 需要模式匹配
│ ├── 数据解构 → match value { Pattern => ... }
│ └── 类型分支 → match value { :TypeName => ... }
├── 需要可变操作
│ ├── 只改字段 → let x = self; x.field = v (无需 mut)
│ └── 重新赋值 → let mut x = self; x = new_x (需 mut)
└── 需要方法/运算符
└── impl Type with method(self, ...) { ... } // 注意:不支持 mut self
执行步骤
Step 1: 分析现有代码
读取目标 .mbt 文件
识别现有类型、函数、import
检查是否有编译错误需先修复(调用 3-debug-errors)
Step 2: 选择代码模式
根据决策树选择合适的模式,参考:
数据类型 → references/syntax.md, references/type-system.md
泛型/Trait → references/generics-traits-methods.md
模式匹配 → references/pattern-matching.md
Step 3: 编写代码(遵循规范)
⚠️ MoonBit v0.9+ 关键规则 :
不支持 mut self 参数 — 用内部绑定替代
For 循环不支持元组解构 — 先绑定再 match
Match 分支多语句必须 {} 包裹 — 不要用逗号分隔
Option 匹配不需要 _ 通支 — Some/None 已穷尽
泛型语法 : fn[T] func_name(...) 不是 fn func_name[T](...)
Step 4: 验证
moon check # 类型检查
moon fmt && git diff --check # 格式化检查
Step 5: 质量检查
代码风格要点
使用 @alias. 完全限定名引用跨包类型(mbtgraph 最佳实践)
同包内直接调用函数,无需模块前缀
Block 用 ///| 分隔组织相关代码
复杂逻辑拆分为私有辅助函数
详细知识
🔗 references/syntax.md — 基础语法速查
🔗 references/type-system.md — 类型系统详解
🔗 references/generics-traits-methods.md — 泛型/Trait/方法完整指南
🔗 references/pattern-matching.md — 模式匹配详解与陷阱
🔗 references/pitfalls.md — 常见陷阱合集
1 --- 2 name: 2-write-code 3 description: 编写/改造 MoonBit 代码 4 --- 5 6 # 编写/改造 MoonBit 代码 7 8 ## 触发条件 9 用户需要**编写或修改** MoonBit 源代码时激活。 10 11 ## 决策树 12 13 ``` 14 代码任务类型? 15 ├── 定义数据类型 16 │ ├── 简单数据 → struct { fields } 17 │ ├── 带变体 → enum { A(...) B(...) } 18 │ └── 需外部构造 → pub(all) struct/enum 19 ├── 需要泛型 20 │ ├── 函数级 → fn[T] func_name(...) 21 │ └── 类型级 → struct[T] Name { ... } 22 ├── 需要多态行为 23 │ ├── 接口抽象 → pub(open) trait { fn method(...) } 24 │ └── 类型枚举 → match expr { :TypeA => ... } 25 ├── 需要模式匹配 26 │ ├── 数据解构 → match value { Pattern => ... } 27 │ └── 类型分支 → match value { :TypeName => ... } 28 ├── 需要可变操作 29 │ ├── 只改字段 → let x = self; x.field = v (无需 mut) 30 │ └── 重新赋值 → let mut x = self; x = new_x (需 mut) 31 └── 需要方法/运算符 32 └── impl Type with method(self, ...) { ... } // 注意:不支持 mut self 33 ``` 34 35 ## 执行步骤 36 37 ### Step 1: 分析现有代码 38 - 读取目标 `.mbt` 文件 39 - 识别现有类型、函数、import 40 - 检查是否有编译错误需先修复(调用 3-debug-errors) 41 42 ### Step 2: 选择代码模式 43 根据决策树选择合适的模式,参考: 44 - 数据类型 → `references/syntax.md`, `references/type-system.md` 45 - 泛型/Trait → `references/generics-traits-methods.md` 46 - 模式匹配 → `references/pattern-matching.md` 47 48 ### Step 3: 编写代码(遵循规范) 49 ⚠️ **MoonBit v0.9+ 关键规则**: 50 1. **不支持 `mut self` 参数** — 用内部绑定替代 51 2. **For 循环不支持元组解构** — 先绑定再 match 52 3. **Match 分支多语句必须 `{}` 包裹** — 不要用逗号分隔 53 4. **Option 匹配不需要 `_` 通支** — Some/None 已穷尽 54 5. **泛型语法**: `fn[T] func_name(...)` 不是 `fn func_name[T](...)` 55 56 ### Step 4: 验证 57 ```bash 58 moon check # 类型检查 59 moon fmt && git diff --check # 格式化检查 60 ``` 61 62 ### Step 5: 质量检查 63 - [ ] 无 E0015 unused_mut 警告 64 - [ ] 无 E4021 unbound variable 错误 65 - [ ] 无 E3002 parse error 66 - [ ] pub/pub(all) 可见性正确 67 68 ## 代码风格要点 69 - 使用 `@alias.` 完全限定名引用跨包类型(mbtgraph 最佳实践) 70 - 同包内直接调用函数,无需模块前缀 71 - Block 用 `///|` 分隔组织相关代码 72 - 复杂逻辑拆分为私有辅助函数 73 74 ## 详细知识 75 🔗 `references/syntax.md` — 基础语法速查 76 🔗 `references/type-system.md` — 类型系统详解 77 🔗 `references/generics-traits-methods.md` — 泛型/Trait/方法完整指南 78 🔗 `references/pattern-matching.md` — 模式匹配详解与陷阱 79 🔗 `references/pitfalls.md` — 常见陷阱合集
morning-start/agent-skills/tree/main/lang/moonbit/skills/2-write-code commit 0b408dc2df
Frequently asked questions How do I install the 2 Write Code skill? Run npx skillmds@latest add morning-start/2-write-code in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the 2 Write Code skill do? 编写/改造 MoonBit 代码 It is listed under Coding & Dev Tools on SkillMD.
Is 2 Write Code safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with 2 Write Code? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is 2 Write Code free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published 2 Write Code? morning-start (@morning-start) published this skill. Their other Agent Skills are listed on their SkillMD profile.