Conditional compilation is for compiling parts of programs in special ways depending on certain compile time conditions. Sometimes, entire sections of a program may need to be taken out and not compiled at all.
Conditional compilation involves condition checks that are evaluable at compile time. Runtime conditional statements like
while are not conditional compilation features.
We have already encountered some features that can be seen as conditional compilations in the previous chapters, including:
unittestblocks are compiled and run only if the
-unittestcompiler switch is enabled.
The contract programming blocks
invariantare activated only if the
-releasecompiler switch is not enabled.
Unit tests and contracts are about program correctness; whether they are included in the program should not change the behavior of the program. The following are the features of D that are specifically for conditional compilation:
We will see these features one by one in this chapter.