Missed scope guard statements

Max Samukha samukha at voliacable.com.removethis
Thu Mar 6 05:37:07 PST 2008


On Thu, 6 Mar 2008 10:47:19 +0000, "Janice Caron"
<caron800 at googlemail.com> wrote:

>On 06/03/2008, Max Samukha <samukha at voliacable.com.removethis> wrote:
>>  if( true ) scope(exit) writef("hello");
>>
>> is semantically the same as simply
>>  if( true ) writef("hello");
>
>For that matter,
>
>    if (true) anything
>
>is the same as
>
>    anything
>

scope is redundant in your example because it is the only statement
within if's scope. It executes a block of statements when the thread
exits if's scope. you could as well omit it.

if (condition)
{
    scope(exit)
   {
		...
   }
}

or without braces (according to you, doesn't work as expected)

if (condition)
    scope(exit)
    {
		...
    }

should be equivalent to

if (condition)
{
	...
}

In case of static if, which doesn't create a new scope:

{
    static if (condition)
       scope (exit)
            writef("hello");

} // hello should be printed when control gets here (not tested)

>But I assume "if (condition)" what what was really intended. It seems
>to me that one could instead write:
>
>    scope(exit) { if (condition) writef("hello") }
>
>That is, reverse the order of "scope" and "if".

{
    bool condition = ...
	
    ...
	
    scope(exit)
    { 
        if (condition) writef("hello");
    }

  ...	
} // hello is printed at this point if condition is true
  // and the function didn't return or throw before
  // the 'scope'



More information about the Digitalmars-d-bugs mailing list