scope(exit) considered harmful

Justin Johansson no at spam.com
Sun Nov 8 10:50:48 PST 2009


Walter Bright Wrote:

> 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.

Incorrect? Hud?  I didn't say that I expected it to execute?

I said

if (false) {
 // .. this code is compiled but ***not*** executed
}

which was my desired behaviour.





More information about the Digitalmars-d mailing list