Conditional compilation
The C preprocessor makes it possible to conditionally compile certain blocks of code using #define
and related directives. Once again, D achieves similar results using built-in compile-time statements, such as version
, debug
, and static if
.
The version condition
A version
condition is used to instruct the compiler to generate code for anything in the version
block only if the specific condition is defined. Here's an example:
version(Windows) pragma(msg, "We are compiling on Windows."); else version(OSX) pragma(msg, "We are compiling on a Mac OS X system."); else version(Posix) pragma(msg, "We are compiling on a Posix system.");
This example uses the predefined versions Windows
, OSX
, and Posix
. Swap the order of the Posix
and OSX
versions and the Posix
block, not the OSX
block, will run on Mac OS X. Remove the else
statements and then both the Posix
and OSX
blocks will compile on Mac. Posix
is defined on all POSIX systems, which...