DMD 0.148 - scope guard

Kyle Furlong kylefurlong at gmail.com
Sun Feb 26 12:20:58 PST 2006


Dawid Ciężarkiewicz wrote:
> 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.

That is definitely *NOT* what I was saying *AT ALL*. Obviously that is a terrible syntax.

Here's what it would look like:

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: (how it should have been)
void LongFunction()
failure
{
	clean_what_foo1_did();
	clean_what_foo2_did();
	clean_what_foo3_did();
	clean_what_foo4_did();
}
body
{
	foo1();
	foo2();
	foo3();
	foo4();
}

I think you missed the point that each function call does not introduce a new scope. Now, its true that if you wanted the 
operations foox() to each be in its own scope, then it would look like this:

void LongFunction()
{
	failure
	{
		clean_what_foo1_did();
	}
	body
	{
		foo1();
	}
	failure
	{
		clean_what_foo2_did();
	}
	body
	{
		foo2();
	}
	failure
	{
		clean_what_foo3_did();
	}
	body
	{
		foo3();
	}
	failure
	{
		clean_what_foo4_did();
	}
	body
	{
		foo4();
	}
}



More information about the Digitalmars-d mailing list