Compile out code for release build in D
Is there any mechanism in D (D2) to force code to be compiled out during a release build?
In C, you might have something like
#ifndef NDEBUG
/*Something that will only run in a debug build*/
#endif
I know that D has
debug(mymodule) {
//Do something
}
But this requires the user to pass -debug for each module to enable it.
I'm looking for 开发者_开发问答a global mechanism that will always run the code in a normal build but compile it out when you pass the -release flag. I know some built-ins have this ability (e.g. assert), but is there any way for user code to do it too?
There is a global notion of debug. Just write:
debug {
... code ...
}
dmd -release -version=dist module.d
and
version(dist) {} else {
int i = 9;
}
Best I can think of.
[update]
Personally, I think the above answer is "bad". The above solution would introduce overly complex logic into the release process, which I think should be straight forward and predictable. I'd recommend just using -debug
and debug{ //... }
. Even if you feel you might forget adding the debug-flag when you're compiling—you're just devving!—mistakes are cheap. Mistakes that make it into the release are worse.
If no better answer is found, a hackaround like this should work: bool debugMode() { bool res; assert(!!(res = true)); return res; }
精彩评论