typestar

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

  1. GRID and CELLS are worked out by the compiler, not at startup.
  2. comptime Pixel names a type, so the SIMD spelling stays short.
  3. main only prints values the compiler already settled.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 5 in Compile-time Mojo, step 1 of 22 in Parameters & SIMD.

Next →