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 ...

Get Learning D now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.