Compile-time constants in Mojo
comptime bindings are folded before the program runs - zero cost at runtime.
comptime GRID = 8
comptime CELLS = GRID * GRID
comptime Pixel = SIMD[DType.uint8, 4]
def main():
print(GRID, CELLS)
var white = Pixel(255, 255, 255, 0)
print(white)
How it works
GRIDandCELLSare worked out by the compiler, not at startup.comptime Pixelnames a type, so theSIMDspelling stays short.mainonly prints values the compiler already settled.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
The run, in numbers
- Lines
- 9
- Characters to type
- 166
- Tokens
- 47
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 35 seconds.
Step 1 of 5 in Compile-time Mojo, step 1 of 22 in Parameters & SIMD.