Loading...
Loading...
仓颉(Cangjie)编程语言开发辅助。当编写、审查或重构仓颉代码时自动激活。 触发条件:检测到 .cj 文件或 cjpm.toml 配置文件。 覆盖场景:语法编写、API 使用、项目构建、性能优化、鸿蒙应用开发。
npx skill4agent add chenchaotao666/cangjie-dev-skill cangjie-devas, abstract, break, Bool, case, catch, class, const, continue,
Rune, do, else, enum, extend, for, func, false, finally, foreign,
Float16, Float32, Float64, if, in, is, init, import, interface,
Int8, Int16, Int32, Int64, IntNative, let, mut, main, macro, match,
Nothing, open, operator, override, prop, public, package, private,
protected, quote, redef, return, spawn, super, static, struct,
synchronized, try, this, true, type, throw, This, unsafe, Unit,
UInt8, UInt16, UInt32, UInt64, UIntNative, var, VArray, where, while| 类型 | 说明 |
|---|---|
| 有符号整数 |
| 无符号整数 |
| 浮点数 |
| 布尔类型 (true/false) |
| Unicode 字符 |
| 字符串 |
| 空类型(类似 void) |
| 底类型 |
// 不可变变量
let x: Int64 = 10
let y = 20 // 类型推断
// 可变变量
var count: Int64 = 0
count = count + 1
// 常量(编译时确定)
const PI = 3.14159// 基本函数
func add(a: Int64, b: Int64): Int64 {
return a + b
}
// 简化写法(单表达式)
func multiply(a: Int64, b: Int64): Int64 {
a * b
}
// 泛型函数
func identity<T>(value: T): T {
value
}
// 带默认参数
func greet(name: String, greeting: String = "Hello"): String {
"${greeting}, ${name}!"
}struct Rectangle {
let width: Int64
let height: Int64
// 构造函数
public init(width: Int64, height: Int64) {
this.width = width
this.height = height
}
// 主构造函数(简化写法)
// public Rectangle(let width: Int64, let height: Int64) {}
// 成员函数
public func area(): Int64 {
width * height
}
// 静态函数
public static func unit(): Rectangle {
Rectangle(1, 1)
}
}// 基类需要 open 修饰符才能被继承
open class Animal {
protected var name: String
public init(name: String) {
this.name = name
}
public open func speak(): String {
"..."
}
}
// 继承
class Dog <: Animal {
public init(name: String) {
super(name)
}
public override func speak(): String {
"Woof!"
}
}interface Drawable {
func draw(): Unit
}
interface Resizable {
func resize(scale: Float64): Unit
}
// 实现多个接口
class Circle <: Drawable & Resizable {
var radius: Float64
public init(radius: Float64) {
this.radius = radius
}
public func draw(): Unit {
println("Drawing circle with radius ${radius}")
}
public func resize(scale: Float64): Unit {
this.radius = this.radius * scale
}
}// 简单枚举
enum Color {
| Red | Green | Blue
}
// 带参数的枚举(代数数据类型)
enum Option<T> {
| Some(T)
| None
}
// 递归枚举
enum Expr {
| Num(Int64)
| Add(Expr, Expr)
| Sub(Expr, Expr)
}
// 使用
let color = Color.Red
let value = Option<Int64>.Some(42)func describe(color: Color): String {
match (color) {
case Red => "红色"
case Green => "绿色"
case Blue => "蓝色"
}
}
// 带解构的模式匹配
func eval(expr: Expr): Int64 {
match (expr) {
case Num(n) => n
case Add(left, right) => eval(left) + eval(right)
case Sub(left, right) => eval(left) - eval(right)
}
}
// if-let 模式
if (let Some(value) <- optionalValue) {
println("Got value: ${value}")
}// 自定义异常
class MyException <: Exception {
public init(message: String) {
super(message)
}
}
// try-catch-finally
func riskyOperation(): Int64 {
try {
if (someCondition) {
throw MyException("Something went wrong")
}
return 42
} catch (e: MyException) {
println("Caught: ${e.message}")
return -1
} finally {
println("Cleanup")
}
}// 创建线程(协程)
let thread = spawn {
println("Running in new thread")
}
// 等待线程完成
thread.join()
// 同步块
var counter = 0
let lock = ReentrantMutex()
func increment(): Unit {
synchronized(lock) {
counter = counter + 1
}
}// 数组
let arr: Array<Int64> = [1, 2, 3, 4, 5]
let first = arr[0]
// ArrayList(可变长数组)
var list = ArrayList<String>()
list.append("hello")
list.append("world")
// HashMap
var map = HashMap<String, Int64>()
map["one"] = 1
map["two"] = 2
// 遍历
for (item in arr) {
println(item)
}
for ((key, value) in map) {
println("${key}: ${value}")
}class Temperature {
private var _celsius: Float64 = 0.0
// getter 和 setter
public prop celsius: Float64 {
get() { _celsius }
set(value) { _celsius = value }
}
// 只读属性
public prop fahrenheit: Float64 {
get() { _celsius * 9.0 / 5.0 + 32.0 }
}
}// 为已有类型添加方法
extend Int64 {
public func isEven(): Bool {
this % 2 == 0
}
}
// 使用
let num: Int64 = 4
println(num.isEven()) // true// 泛型类
class Box<T> {
private var value: T
public init(value: T) {
this.value = value
}
public func get(): T {
value
}
}
// 泛型约束
func compare<T>(a: T, b: T): Bool where T <: Comparable<T> {
a < b
}my-project/
├── cjpm.toml # 项目配置文件
├── src/
│ └── main.cj # 主源文件
├── test/
│ └── main_test.cj # 测试文件
└── build/ # 构建输出[package]
name = "my-project"
version = "1.0.0"
description = "My Cangjie Project"
[dependencies]
# 依赖配置# 创建项目
cjpm init my-project
# 构建
cjpm build
# 运行
cjpm run
# 测试
cjpm test
# 格式化代码
cjfmt -w src/
# 代码检查
cjlint src/MyClassmyFunctionmypackage/**// 调用 C 函数
@Foreign
foreign func printf(format: CString, ...): Int32
// 在 main 中使用
main() {
unsafe {
printf("Hello from C!\n".toCString())
}
}| 错误 | 原因 | 解决方案 |
|---|---|---|
| 变量未初始化 | 在使用前赋值 |
| 类型不匹配 | 检查类型或添加类型转换 |
| 修改 let 变量 | 改用 var 声明 |
| struct 递归定义 | 使用 class 代替 |
docs/syntax/docs/stdlib/docs/tools/docs/examples/