DMD 0.148 - scope guard

Dawid Ciężarkiewicz dawid.ciezarkiewicz at gmail.com
Sun Feb 26 11:47:44 PST 2006


Kyle Furlong wrote:
> for any scope:
> 
> in
> {
> }
> out
> {
> }
> failure
> {
> }
> success
> {
> }
> body
> {
> }
> 
> Pros: fits into and expands current language structures
> Cons: still a bit verbose as compared to current syntax

I'm sorry to say but IMO this miss the whole point.

Current keywords: in, out, body are place-insensitive . They are just blocks
that may be moved relatively for themselves and this will not change
anything. The whole point in "scope guarding" is that expression you type
_registers_ some piece of code (to be called in some conditions) in place
where it appears.

You gain nothing with such syntax. If you want to deal with whole scope then
you can use finally and catch blocks.

Examples:

Current syntax:
void LongFunction()
{
    foo1();
    on_scope_failure clean_what_foo1_did();

    foo2();
    on_scope_failure clean_what_foo2_did();

    foo3();
    on_scope_failure clean_what_foo3_did();

    foo4();
}


What I understand from your proposal:
void LongFunction()
{
    foo1();
    failure {
        on_scope_failure clean_what_foo1_did();
    }
    body {

       foo2();

       failure {
           on_scope_failure clean_what_foo2_did();
       }
       body {
            foo3();
       
            failure {
               on_scope_failure clean_what_foo3_did();
            }
            body {
                foo4();
            }
       }
    }

}

If you think about:
void LongFunction()
{
    foo1();
    failure{
      clean_what_foo1_did();
    }

    foo2();
    failure {
     clean_what_foo2_did();
    }
    
    foo3();
    failure {
     on_scope_failure clean_what_foo3_did();
    }

    foo4();
}

Then concept is OK, but you can no longer say this "will fit and expand
current syntax", because it's used totally diffrent from  body {} in {} out
{} blocks.



More information about the Digitalmars-d mailing list