Macro pragmas

All macros and templates can also be used as pragmas. They can be attached to routines (procs, iterators, etc), type names or type expressions. The compiler will perform the following simple syntactic transformations:

  1. template command(name: string, def: untyped) = discard
  2. proc p() {.command("print").} = discard

This is translated to:

  1. command("print"):
  2. proc p() = discard

  1. type
  2. AsyncEventHandler = proc (x: Event) {.async.}

This is translated to:

  1. type
  2. AsyncEventHandler = async(proc (x: Event))

  1. type
  2. MyObject {.schema: "schema.protobuf".} = object

This is translated to a call to the schema macro with a nnkTypeDef AST node capturing both the left-hand side and right-hand side of the definition. The macro can return a potentially modified nnkTypeDef tree which will replace the original row in the type section.

When multiple macro pragmas are applied to the same definition, the compiler will apply them consequently from left to right. Each macro will receive as input the output of the previous one.