scope(exit) considered harmful

Don nospam at nospam.com
Sun Nov 8 10:59:30 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.

if (true) scope (exit) writeln("hello world!");
// doesn't execute either! -- bugzilla 1894, see also 3323 and 1087.
Probably not good to encourage people to use these right now :-(.







More information about the Digitalmars-d mailing list