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).
pragma | description |
---|---|
intdefine | Reads in a build-time define as an integer |
strdefine | Reads in a build-time define as a string |
booldefine | Reads in a build-time define as a bool |
const FooBar {.intdefine.}: int = 5
echo FooBar
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.
const FooBar {.intdefine: "package.FooBar".}: int = 5
echo FooBar
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.