Version: 1.1.0

Parameterization

Parameters are compile-time inputs of a function or struct. They appear in square brackets and become constants in the body.

Essentials

  • fn scale<a href="/value: Float64/">n: Int</a> is parameterized by n.
  • Call it as scale<a href="/1.5/">4</a>.
  • A struct parameter changes the type: SIMD[DType.float32, 8] is a different type from width 4.
  • Parameters can have default values.
  • The compiler builds a specialized copy for each distinct parameter set.

Example

struct Buffer[size: Int]:
    var data: InlineArray[Float64, size]

fn main():
    var buf = Buffer[128]()
    print(len(buf.data))

Keep in mind

Parameterization is how one kernel covers many dtypes and tile sizes without a runtime switch.