Skip to content

comptime block

The most direct form of compile-time evaluation is the comptime block. You write comptime { ... } just as you would any code block: declare variables, perform calculations, write the final expression without a semicolon as the return value. The compiler executes that block and replaces the entire expression with the resulting value — the running program never sees the block, only the literal.

This means the computational cost of the block does not exist in production: every operation happens exactly once, at compile time.

Basic comptime block: two variables and a multiplication baked as a literal.

01-block.zolo
Playground
// Feature: comptime block — evaluate at compile time, bake the result
// Syntax: `comptime { ... <tail expr> }` runs in the compiler and
// substitutes the final value into the program.
// When to use: precompute lookup tables, expensive constants,
// configuration baked from build flags. Zero runtime cost.

let answer = comptime {
    let a = 6
    let b = 7
    a * b
}

print(answer)
// expected: 42

Challenge

Replace the values of a and b with math.sqrt(144.0) and math.sqrt(49.0) and observe that stdlib functions also work inside a comptime block.

enespt-br