When nimvm statement

nimvm is a special symbol, that may be used as expression of when nimvm statement to differentiate execution path between compile time and the executable.

Example:

  1. proc someProcThatMayRunInCompileTime(): bool =
  2. when nimvm:
  3. # This branch is taken at compile time.
  4. result = true
  5. else:
  6. # This branch is taken in the executable.
  7. result = false
  8. const ctValue = someProcThatMayRunInCompileTime()
  9. let rtValue = someProcThatMayRunInCompileTime()
  10. assert(ctValue == true)
  11. assert(rtValue == false)

when nimvm statement must meet the following requirements:

  • Its expression must always be nimvm. More complex expressions are not allowed.
  • It must not contain elif branches.
  • It must contain else branch.
  • Code in branches must not affect semantics of the code that follows the when nimvm statement. E.g. it must not define symbols that are used in the following code.