www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - version(assert)

reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
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?
Sep 02 2012
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 02-09-2012 13:39, Peter Alexander wrote:
 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?
(Sorry, managed to reply directly via email with the previous message.) Yes: https://github.com/D-Programming-Language/dmd/pull/1099 (If that's accepted, I'll send patches to GDC and LDC as well.) -- Alex Rønne Petersen alex lycus.org http://lycus.org
Sep 02 2012
parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Sunday, 2 September 2012 at 12:49:39 UTC, Alex Rønne Petersen
wrote:
 Yes: https://github.com/D-Programming-Language/dmd/pull/1099

 (If that's accepted, I'll send patches to GDC and LDC as well.)
Thanks, was going to do it myself if there was interest, but you've saved me the effort :-) Also, I have now realised that version(release) doesn't work...
Sep 02 2012