Idea: break/continue and scope guards

BCS BCS at pathlink.com
Fri Jul 6 16:17:14 PDT 2007


mike wrote:
> 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.

scope break would not match a throw. If the search logic is more complex 
  than the above (it might be able to tell that there isn't going to 
find something) then this wouldn't work.

Also it should be pointed out that the example is a bit silly unless 
there is more than one break.

' foreach (customer; customerList)
'     if (customer.ID == wantedID)
'     {
'        result = customer;
'        break;
'     }

vs.

' forLunch foreach (customer; customerList)
' {
'     scope(break) result = customer;
'     if (customer.ID == wantedID) break;
'     // butch of processing
'     if (customer.OtherStat == wantedStat) break;
'     // more stuff
'     {if(now is lunch.time) break forLunch ;}
'     //...
' }


> 
> Anyway, I think you forgot scope(return) :)
> 

:b

however it has all the issues of scope(outer.exit) / scope(exit lable)

> -mike



More information about the Digitalmars-d mailing list