About switch case statements...

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sun Nov 15 11:20:51 PST 2009


Andrei Alexandrescu wrote:
> Chad J wrote:
>> So, switch-case statements are a frequent source of nasty bugs.  Fixing
>> them (well) requires breaking backwards compatibility.
>>
>> Any chance this will happen for D2?
>>
>> (This is intended as more of a reminder and simple curiosity than a
>> discussion.)
> 
> I wish very much that a transferring control flow statement (break, 
> return, goto etc.) is required at the end of each case. Then, the rare 
> case when you want to break through is easy to implement as goto case 
> xxx; and all is good.
> 
> Walter's answer to that has put me to silence for good. "But I use 
> fall-through all the time!" I then knew the feature will never make it.
> 
> 
> Andrei

For what its worth, in a (very domain specific) scripting language I've been working on, I 
used 'continue' to implement fall-through, and haven't felt too bad about it.  That and 
being able to do (case A, B, C:) helps for the most common (IME) sort of use.

//////////////////////////////////////////////////
.__receive system ( cmd: STR, @params )
     switch ( cmd .lowercase .tosym )
         case 'lo', 'login':
             user.tell( "(LOgin has been deprecated; please start using COnnect instead.)")
             continue;

         case 'co', 'connect':
             uname, pwd <- params;
             who = #Database::User.lookup( uname )
             if ( who.valid && who.accept_password( pwd ) )
		return .accept( user, who );
             end

         // ... and so on ...
     end
end
//////////////////////////////////////////////////

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list