10. comptime and const

  • chapter 10 of 20
  • 1 min read

comptime { ... } is executed at compile time by the interpreter and its result is burned into the constant pool — there are no macros. A top-level const is named in upper case, and its initializer is implicitly 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))
55

See also: spec.en.md §7