switch()

Manu turkeyman at gmail.com
Mon Feb 17 20:05:51 PST 2014


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

> On 2/17/2014 5:49 AM, Manu wrote:
>
>> Refer to my other reply wrt the 'rubble' concept.
>>
>
> Sure :-)
>
>
>
>  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.
>>
>
> I have a real hard time seeing the proposed changes as radical or game
> changing.
>
>
>
>  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.
>>
>
> foreach() is quite a large improvement, because by abstracting away the
> mechanics of iteration, code need no longer be aware of just what abstract
> type is being looped over. Furthermore, it eliminates several common
> sources of coding bugs.
>
> I'm not seeing this with changes in switch.
>

I think abstracting away volumes of comparative matching logic is similarly
interesting to eliminating tedious iteration logic. I find out-by-ones and
bogus comparisons that I didn't spot is a very common class of bug (often
just because matching code is long and repetitive).
Cut and paste, oops; you didn't spot that <= is actually >= from where you
cut... or you didn't see it because the line is so long with repetition of
the terms being compared that it's run off the right edge.
*disclaimer: peoples experiences may differ, this is my experience.

I agree, it's probably not as game-changing as foreach, but I think if you
had it for a few years and became accustomed to writing much of that
comparative logic in a simple and really obvious/less-repetitive way, you'd
look back and wonder how you did without it. I suspect it's possible that
the repetitive matching logic often found in if() statements represent a
lot more code by volume than the iteration logic eliminated by foreach ever
did in for loops.

I can't say how big a deal it is until I make a habit of applying it
everywhere, but I can imagine the potential for massive improvement in
readability, and elimination of many long if() expressions.

Comparing the savings:

for(int i=0; i<things.length; ++i)
->
foreach(i, thing; things)


if(thing.member >= min && thing.member < max)
->
match(thing.member)
 case(min..max)

If an 'else if' appears, the savings compound considerably.


I'm a practicality motivated programmer, I don't dream about futuristic
features as much as I respond to the things that annoy me consistently on a
daily basis. I don't like friction between me and getting my work done, and
it's the little things that come up most often that tend to really bug me.
I'd like to think the languages foundational keywords should offer a good
experience. You'll be typing them over and over again from now until the
day you die. Looking at other languages, I think there's clearly a missed
opportunity to do something a lot more modern and useful with the switch
concept.

Rust does it nice. It's got a nice functional flavour to it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140218/ce4e7de4/attachment-0001.html>


More information about the Digitalmars-d mailing list