version(assert)

Peter Alexander peter.alexander.au at gmail.com
Sun Sep 2 04:39:08 PDT 2012


The language spec says:

"The compiler may optionally not evaluate assert expressions at 
all."

In practice, this means that it depends on compiler flags. For 
DMD, assert expressions are ignored in -release.

Sometimes, I want to do things only when asserts are being 
compiled, for example:

version(release) {} else import std.format;
assert(x == 0, format("x is %d, not 0", x));


I have two problems with this:

1. It's ugly to use the version... else syntax.
2. It's tied to the fact that DMD doesn't compile asserts in 
release.

What I would prefer:

version(assert) import std.format;


Of course, version(assert) is compiled when asserts are compiled.

This makes the intent clear, and removes coupling with particular 
compiler options.

Thoughts?


More information about the Digitalmars-d mailing list