Note: This website is archived. For up-to-date information about D projects and development, please visit

Conditional Compilation (a/k/a Versioning) Examples

Examples That Use Version in Other Categories

Versioning is very straight forward in D

(adapted from NG:digitalmars.D/17398)

Versioning is very straight forward in D.

For example:

    // code here gets compiled if version XX is active

   // code here gets compiled if version YY is active
   version(XX) {
      // Both YY and XX must be active for this stuff to compile.

You can set the versions by the command line -version= switch.

Some versions are built-in and are already active.

You can also set versions during the compile process from within your code...

// Set Posix version active if either linux or darwin is active.
version(linux) version=Posix;
version(darwin) version=Posix;

You can also set version levels. This means that the code will only compile if the current version level is greater than or equal to the one specified in the code. The assumption is that higher versions include code that applied to earlier versions (i.e. Compiling for version 4 must also include stuff that was created for version 3 and earlier).

   // code here only gets compiled if the version has been
   // set to 2 or higher.

To get this effect into action, invoke dmd thus ...

dmd myapp -version=2

It can also be used to comment out code ...

version(none) {
   // this code is now commented out 

This is useful because it is easy to nest versions to make commenting out code easier.

   version(none) {

More Information

See: Conditional Compilation in the D Specification.