noSideEffect pragma

The noSideEffect pragma is used to mark a proc/iterator to have no side effects. This means that the proc/iterator only changes locations that are reachable from its parameters and the return value only depends on the arguments. If none of its parameters have the type var T or out T or ref T or ptr T this means no locations are modified. It is a static error to mark a proc/iterator to have no side effect if the compiler cannot verify this.

As a special semantic rule, the built-in debugEcho pretends to be free of side effects, so that it can be used for debugging routines marked as noSideEffect.

func is syntactic sugar for a proc with no side effects:

  1. func `+` (x, y: int): int

To override the compiler’s side effect analysis a {.noSideEffect.} cast pragma block can be used:

  1. func f() =
  2. {.cast(noSideEffect).}:
  3. echo "test"