Odds and ends

In this section, we're going to look at several compile-time features that don't fit snugly in the preceding sections.

static assert

static assert is a compile-time assertion that is always enabled. It can't be turned off with -release like a normal assert can. It is not affected by runtime conditionals. When its condition is evaluated to false, an error is generated and compilation halted. The following static assert always errors out because its condition is 0.

void main() {
    if(0) static assert(0);
}

Like a normal assert, it's possible to give a static assert a message to be printed on failure. The following example is a good use of that feature:

version(Windows) enum saveDirectory = "Application Name"; else version(Posix) enum saveDirectory ...

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.