O'Reilly logo

Learning D by Michael Parker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required