Adding finally to switch

Bill Baxter dnewsgroup at billbaxter.com
Tue Apr 1 01:27:14 PDT 2008


Bill Baxter wrote:
> Henning Hasemann wrote:
>> Jesse Phillips <jessekphillips at gmail.com> wrote:
>>> I haven't given it much thought, but I figured I'd let some other
>>> people look at it too.
>>>
>>> Switch statements are nice, many people hate having to use break; all
>>> the time, but I don't and am not interest in the debate. What I think
>>> is missing from a switch statement is a finally section. Most of the
>>> time I don't have a use for the fall-through feature of switch, but I
>>> do have a use for doing one or more things that are the same in every
>>> case.
>>
>> I don't think I get your point. If you have code that is the same in
>> *every* case why cant you do:
>>
>> do_this_before_each_case();
>> switch(foo) {
>>  // ...
>> }
>> do_this_after_each_case();
>>
>> Henning
>>
> 
> Maybe he meant every case except for the default?  Still not that big a 
> deal, though.  Something like this works:
> 
> bool passed=false;
> switch(foo) {
>    ...
>   default:
>      passed = true;
> }

// Doh! Works better if you remember to check the condition
if(!passed)
> do_this_after_each_non_default_case();


-bb



More information about the Digitalmars-d mailing list