10. comptime 与 const

comptime { ... } 在编译期由解释器执行,结果烧进常量池——没有宏。 顶层 const 名字用全大写,其初始化隐式是 comptime:

fn fib(n: Int) -> Int =
  if n < 2 { n } else { fib(n - 1) + fib(n - 2) }

const FIB10: Int = comptime { fib(10) }

pub fn main() -> Unit !io =
  println(to_string(FIB10))
在 Playground 打开
55