Version: 1.1.0

Constraints

Constraints are compile-time assertions about parameters. They fail the build when a caller asks for a combination you cannot implement.

Essentials

  • comptime assert stops compilation with a message.
  • constrained[cond, msg]() is the library form of the same check.
  • Put checks at the start of the function so the message points at the caller.
  • Prefer a trait when the requirement is a method, and a constraint when it is a numeric fact.

Example

fn tile[size: Int]():
    comptime assert size > 0, "tile size must be positive"
    print(size)

Keep in mind

A good constraint message names the parameter and the legal range.