used pragma

Nim produces a warning for symbols that are not exported and not used either. The used pragma can be attached to a symbol to suppress this warning. This is particularly useful when the symbol was generated by a macro:

  1. template implementArithOps(T) =
  2. proc echoAdd(a, b: T) {.used.} =
  3. echo a + b
  4. proc echoSub(a, b: T) {.used.} =
  5. echo a - b
  6. # no warning produced for the unused 'echoSub'
  7. implementArithOps(int)
  8. echoAdd 3, 5

used can also be used as a top level statement to mark a module as “used”. This prevents the “Unused import” warning:

  1. # module: debughelper.nim
  2. when defined(nimHasUsed):
  3. # 'import debughelper' is so useful for debugging
  4. # that Nim shouldn't produce a warning for that import,
  5. # even if currently unused:
  6. {.used.}