Version: 1.1.0
Compile-time evaluation
comptime runs code during compilation and leaves a constant behind. Loops, conditions, and pure calculations are all available.
Essentials
comptime ifkeeps one branch and deletes the other.comptime forunrolls a loop whose trip count is a parameter.- A
comptimevalue can become a runtime value by using it in ordinary code. - Side effects that need a running process do not belong in
comptime.
Example
fn pow2[n: Int]() -> Int:
var acc = 1
comptime:
for _ in range(n):
acc *= 2
return acc
Keep in mind
Unrolling helps tiny kernels. It hurts when the generated code gets larger than the instruction cache.