Idea: break/continue and scope guards
mike
vertex at gmx.at
Fri Jul 6 14:33:55 PDT 2007
BCS Wrote:
> This is much like an idea I had, but backwards from the way I thought of
> it. Add more options to scope, not break/continue.
>
> the second code example would work like this the way I thought of.
>
> ' foreach (customer; customerList)
> ' {
> ' scope(break) result = customer;
> ' if (customer.ID == wantedID) break;
> ' }
>
>
> 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.)
> 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.
Never thought of that. That's an interesting idea. One thing wouldn't work though: I see only two reasons to break a loop - either you found what you were looking for and don't need to iterate over the rest, or there was an error and you abort the loop. That's why I thought break(success/failure) would be great. scope(break) would execute no matter if it was a "good" break or a "bad" one.
Anyway, I think you forgot scope(return) :)
-mike
More information about the Digitalmars-d
mailing list