Idea: break/continue and scope guards

Robert Fraser fraserofthenight at gmail.com
Fri Jul 6 12:11:59 PDT 2007


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.



More information about the Digitalmars-d mailing list