compile-time define pragmas

The pragmas listed here can be used to optionally accept values from the -d/—define option at compile time.

The implementation currently provides the following possible options (various others may be added later).

pragmadescription
intdefineReads in a build-time define as an integer
strdefineReads in a build-time define as a string
booldefineReads in a build-time define as a bool
  1. const FooBar {.intdefine.}: int = 5
  2. echo FooBar
  1. nim c -d:FooBar=42 foobar.nim

In the above example, providing the -d flag causes the symbol FooBar to be overwritten at compile-time, printing out 42. If the -d:FooBar=42 were to be omitted, the default value of 5 would be used. To see if a value was provided, defined(FooBar) can be used.

The syntax -d:flag is actually just a shortcut for -d:flag=true.

These pragmas also accept an optional string argument for qualified define names.

  1. const FooBar {.intdefine: "package.FooBar".}: int = 5
  2. echo FooBar
  1. nim c -d:package.FooBar=42 foobar.nim

This helps disambiguate define names in different packages.

See also the generic `define` pragma for a version of these pragmas that detects the type of the define based on the constant value.