Conditional compilation for non-release version
bearophile
bearophileHUGS at lycos.com
Sat Apr 28 05:49:41 PDT 2012
Joseph Rushton Wakeling:
> Is there a way to put in place a conditional segment of code
> that is included if the code is _not_ compiled with the
> -release flag?
Contract programming? D is designed to make it hard on purpose to
do what you want to do.
> The reason I'm asking is because the checks I want to perform
> are of the form,
>
> foreach(x; myVeryLargeArray) {
> assert(/* ... some condition about x */);
> }
>
> ... and I'm concerned that with the foreach() loop in place, it
> will slow down the code even if the assert() statements are
> ignored at compile time. So I'd like to be able to do
> something instead like,
>
> version(!release)
> {
> foreach(x; myVeryLargeArray) {
> assert(/* ... some condition about x */);
> }
> }
>
> ... is this possible?
My suggestion is to use a pure helper predicate, possibly nothrow
too (a pure lambda too is OK):
bool isGood(MyType x) pure {
...
}
foreach(x; myVeryLargeArray) {
assert(isGood(x));
}
The purity of the predicate is almost necessary, to be sure your
program behavior doesn't change between release and non-release
mode.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list