switch()

Ary Borenszweig ary at esperanto.org.ar
Tue Feb 18 09:52:41 PST 2014


On 2/18/14, 2:26 PM, Daniel Murphy wrote:
> "Steven Schveighoffer"  wrote in message
> news:op.xbhkirppeav7ka at stevens-macbook-pro.local...
>
>> My point though, is that the change to require default gains you
>> nothing except annoyed programmers. Why put it in?
>
> It only gains you nothing if you respond to the error by mindlessly
> putting a default: break; in.
>
> The compiler is trying to help you by getting you to take an extra
> second and explicitly state what you what.  If you are automatically
> silencing the error without thinking about the semantics you want, you
> absolutely should be irritated, but the compiler is not the one doing
> something stupid here.
>
> The awful part of checked exceptions comes from two place IMO - the ide
> gives you a very very easy way to do thing wrong thing (two clicks
> IIRC), and doing the right thing is often very very difficult (change
> exception lists on every calling functions).
>
> Here the right and wrong choice are about equal difficulty - trivial.  I
> think complaints about typing those extra couple dozen keystrokes are on
> the same level as "why do I have to put these ';'s everywhere" and
> "'immutable' has too many letters".

The compiler should force you to write an "else" for every if then:

if (a == 2) {
   // do something
}

Error: missing else

if (a == 2) {
   // do something
} else {
   nothing;
}

Or maybe:

if (a == 2) {
   // do something
} else {
   assert(0);
}

I think the main complaint is that if you refactor a chain of if/else-if 
into a switch statement, then you have to add a "default: break;" for 
sure, which is just redundant.


More information about the Digitalmars-d mailing list