switch()

Manu turkeyman at gmail.com
Mon Feb 17 05:49:54 PST 2014


On 17 February 2014 18:43, Walter Bright <newshound2 at digitalmars.com> wrote:

> On 2/16/2014 7:39 PM, Manu wrote:
>
>> I think a really useful construct could be made out of switch, but it
>> seems that
>> it won't happen because it must support C code unchanged.
>>
>
> I tend to agree with Andrei on this - the proposals aren't fundamental or
> game changing, and are kinda just bouncing the rubble around (po-tay-to vs
> po-tah-to).


Refer to my other reply wrt the 'rubble' concept.

I think a quality implementation would be fairly game changing. A properly
scoped and fully featured switch/select/match statement would result in
some radical simplifications of code all over the place.
Sequential if/if else statements might see a decline, and there would be
far less repetition of terms in the conditional statements.
If switch (or something like it) were massaged to be as nice to use as
foreach is, I think you'll find it will be used all over the place.

Note: I probably wouldn't be exaggerating if I said foreach is the main
reason I started looking into D in the first place. When I first glanced
over the D feature list, I could have easily closed the browser tab, but
that's the thing that kept me reading...
I've said it before, and I'll say it again, I think these things are _so
fundamental_, that their importance is often underestimated, and often
overlooked.
Could you imagine D without foreach? In retrospect, you can't. But If you
were arguing from a position where you never had anything like foreach to
the value of it's inclusion, it wouldn't look anywhere near as strong as it
does in retrospect.

The idea of eliminating many unsightly if/else if blocks is very appealing
to me.
Just scanning through my code, I can imagine so many instances where my
code would become more readable.
People usually use switch today for the same reason, but it's always a hard
call... is the crappiness of 'switch' a quantifiable improvement over the
if/else if sequence? I find myself asking that question all the time.
That's why I brought it up.


> Isn't that the entire point of final switch?
> > Why introduce final switch to address that, then do this aswell?
>
> Implicit in your questions is switches will only be used on enums, i.e.
> all the values a type can be are known to the compiler. This is only true
> for a smallish subset of types that are switched on.
>
> final switch : for enums
>
> default : for everything else
>

But... that's like making it an error for an if() to appear without an else
statement. It's _exactly_ like that.
I don't think I couldn't disagree more.

Imagine:

if(condition)
{
  doSomething();
}
else
{}

blah blah.

if(failed)
{
  handleError();
}
else
{}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140217/add3e3ba/attachment-0001.html>


More information about the Digitalmars-d mailing list