DMD 0.148 - scope guard

James Dunne james.jdunne at gmail.com
Sun Feb 26 13:05:48 PST 2006


Julio César Carrascal Urquijo wrote:
> James Dunne wrote:
> 
>> Kyle Furlong wrote:
>> bool LongFunction()
>> {
>>     bool  passed = false;
>>
>>     scope (State save = UIElement.GetState())
>>     catch { UIElement.SetState(Failed(save)); writef('0'); }
>>     pass { UIElement.SetState(save); writef('1'); }
>>     pass { passed = true; writef('2'); }
>>     body {
>>         ... lots of code ...
>>     }
>>     exit { writef('3'); }
>>
>>     return passed;
>> }
> 
> 
> This is the best I've seen but:
> 
> - Why two "pass" blocks?.

Just to demonstrate that the ordering of the blocks matters, and that 
multiple blocks can be defined.

> - About "exit" would be better before the "body" block and maybe replace 
> it with "finally" to save one more keyword.

I noted this was a matter of personal style where the body block goes, 
as it does not depend on the order of the other blocks.  Personally, I 
like seeing the exit block after the body, as it should flow naturally.

> 
> I'm also assuming that we could also do one liners without the braces:
> 
> 
> bool LongFunction()
> {
>     bool  passed = false;
> 
>     scope State save = UIElement.GetState();
>     catch UIElement.SetState(Failed(save));
>     pass UIElement.SetState(save);
>     finally writef('3');
>     body {
>         ... lots of code ...
>     }
> 
>     return passed;
>  }

Sure, why not?

-- 
Regards,
James Dunne



More information about the Digitalmars-d mailing list