Idea: break/continue and scope guards
BCS
BCS at pathlink.com
Fri Jul 6 13:13:01 PDT 2007
Robert Fraser wrote:
> BCS Wrote:
>
>>[...]
>>other scope "types":
>>goto // exit by goto
>>goto : label // exit by goto to a given label
>>continue // duh
>>enter // execute on entrance by any means (goto, switch, etc.)
>
>
> scope(enter) might be a bit problematic, since statements are executed in lexical order.
>
>
>>out // used in a loop same as exit but for scope including
>> // loop. Note: scope(exit) in a loop runs on each cycle,
>> // 'out' would only run at the end of the last cycle.
>
>
> How about an "outer.xxx" too? For example, in a loop, "scope(outer.failure)" which would execute when the loop-enclosing scope exits via failure? You can have arbitrarily nested scopes (i.e. outer.outer.outer.goto), and have the exit clause execute at the end of any scope currently accessible.
>
> The only problem I can see with this is there seems to be a good potential for abuse, especially if arbitrary labels could be specified. I think it could lead to that "spaghetti code" problem that would make it really hard to figure out what's going on without a debugger, while the whole point of scope is to group logically connected code together. But I guess that's up to the coder to determine where to use each technique. I'm just afraid the more nice little features like this you get in a language, the more everyone's coding style differs.
Using labeled scopes would be better IMHO:
L : for(int i = 5; i; i--)
for(j=0; j<i; j++)
{
scope(exit L) foo();
}
this does however does strange things with scope when because it lest
scope reach out to a scope that is outside the current one
L : for(int i = 5; i; i--)
{
if(cond()) scope(exit L) foo();
// what happens when cond() == false
}
Also what happens when a single scope is run more than one time before
the scope is exited?
It should be noted that scope is currently implemented as sugar around a
try/catch/finally.
More information about the Digitalmars-d
mailing list