Static statement/expression

A static statement/expression explicitly requires compile-time execution. Even some code that has side effects is permitted in a static block:

  1. static:
  2. echo "echo at compile time"

static can also be used like a routine.

  1. proc getNum(a: int): int = a
  2. # Below calls "echo getNum(123)" at compile time.
  3. static:
  4. echo getNum(123)
  5. # Below call evaluates the "getNum(123)" at compile time, but its
  6. # result gets used at run time.
  7. echo static(getNum(123))

There are limitations on what Nim code can be executed at compile time; see Restrictions on Compile-Time Execution for details. It’s a static error if the compiler cannot execute the block at compile time.