scope(exit) considered harmful
Walter Bright
newshound1 at digitalmars.com
Sun Nov 8 10:42:37 PST 2009
Justin Johansson wrote:
> Long ago in the days before D, I was used to commenting out code with one of
> two idioms:
>
> #if false
> .. this code is not compiled
> #endif
>
> or
>
> if (false) {
> .. this code is compiled but not executed
> }
>
> and to turn stuff back on again:
>
> #if true
> .. this code is back on now
> #endif
>
> or
>
> if (true) {
> .. this code is back on now too
> }
>
> but in the D world beware the Ides of Scopes because if your curly
> brace block with the true/false conditional contains a scope(exit)
> you will get explainable but unexpected behaviour if this code
> is enabled inside an if(true) block.
>
> Gotcha.
This is incorrect. Given:
if (false)
{
scope(exit) writeln("hello world!");
}
The writeln will never execute.
More information about the Digitalmars-d
mailing list