Skip to Content

施工中

这一篇还在编写,内容与示例随时可能变动。

常量方法(Const Func)

const func 是编译期可求值的函数:函数体由编译器在编译期间执行,结果物化为常量。它是「普通函数 + 常量能力」双面:运行期照常调用,常量上下文走体解释。

编译期求值

const func Fib(n: int) -> int { if n < 2 { return n; } return Fib(n - 1) + Fib(n - 2); } const Answer: int = Fib(10); const Greet: string = "常" + "量"; func main() -> int { print(Answer, Greet); return 0; }
55 常量

Fib(10) 在编译期算完,Answer 直接是 55。const 初值还能引用先前 const,编译器沿链求值。

编写到此Written up to here

本页其余部分尚未完成,内容随时补充。The rest of this page is not written yet; more content will follow.

最后更新于 2026年10月9日