switch()

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Feb 17 08:31:57 PST 2014


On 2/17/14, 5:33 AM, Manu wrote:
> On 17 February 2014 16:18, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org <mailto:SeeWebsiteForEmail at erdani.org>>
> wrote:
>
>     On 2/16/14, 7:42 AM, Manu wrote:
>
>         So D offers great improvements to switch(), but there are a few
>         small
>         things I wonder about.
>
>
>     TL;DR of my answer to this: at some point we must get used to the
>     notion that minute syntax tweaks are always possible that make us
>     feel we're making progress when instead we're just moving the rubble
>     around.
>
>
> OT: Do you realise how harsh your posts often appear to people? I often
> find I need to restrain myself when replying to your blunt dismissals of
> peoples opinions/suggestions.

You're right. I shouldn't have answered because now the thread is 
fueling from the answers it's getting. Well actually I did my best in my 
post to sugarcoat my opinion of the post, which I found a whine on minor 
issues and a collection of entirely petty proposals. Sorry.

Advanced pattern matching and safe downcasting and such - that may be 
valuable to discuss. But the concerns expressed in this thread build a 
store on saving on typing 'case' and 'break'.

> So, you admit that this is a disaster site, but are reluctant to
> consider what good may come of it?

I didn't admit that. (And I'm sure this is not an attempt to put words 
in my mouth.) That wasn't even the charge. I'll actually quote this 
thread's overture: "So D offers great improvements to switch(), but 
there are a few small things I wonder about."

> I'm not suggesting to move the rubble around a bit, I'm suggesting a
> missed opportunity to build something new and useful from the rubble.

I believe even if we implement all of these suggestions (change range 
syntax, "break" no longer required, no need for "default") to 
perfection, they would have zero consequence on the success of D. I 
really do believe that.

> There are like, 5 control statements in the language; if, for, while,
> foreach, switch. To trivialise their importance is surprising.

It would be surprising. But nobody is.

> switch is basically the same in D as in C, which kinda finds a purpose
> here and there, but it's built on a rubbish foundation. I can imagine
> switch (or something like it) being _extremely_ useful to address whole
> new classes of problems (like match in rust and others) if it were just
> a little bit less shit.

A match statement would be interesting, but was not asked for. This 
thread wrings hands over punctuation and formatting.

[snip some of the said hand wringing]

>     I think the current switch statement design is a fine design all
>     things considered (compatibility, law of least surprise, usability,
>     readability).
>
>
> Well, I disagree. It offers some improvement over the catastrophic
> design that C gave us almost half a century ago, but it doesn't address
> the basic un-structured nature of the statement, and deliberately
> retains all the elements that made it rubbish back when (for compatibility).
>
> Compatibility and law of least surprise I'll grant you.
> Usability and readability, I don't think so. Certainly not compared to
> competition.
>
> I think what's clear though, is in order to not violate the first 2
> (which I agree there is merit in maintaining), any improvement to switch
> would probably need to come in the form of a new structure, like 'match'
> in rust, or something designed from the ground-up to not suck, 'select'
> maybe.
>
> I concede that switch can't be fixed. Perhaps it should be deprecated
> instead.

I don't think we should deprecate switch. And if the to-be-proposed 
match/select's main features are small syntactic fixes to switch, I 
don't think they're worth adding.

> So I guess on that note, I'll leave it there, but I still think it's
> worth consideration.

Improving on "switch" is hardly the kind of thing that keeps me awake at 
night. Finalizing the language is. Threads. Garbage. Null pointers. 
this(this). Allocators. Dynamic libraries. Better punctuation for 
"switch" is not even down the list - it's not on the list. And I don't 
think it should. If that is, anything could be put on the list by the 
loudest voice asking.

> The functionality that switch offers is tremendously useful, and it
> would be so much more useful if it were cleaned up; people would
> actually use it, and probably in a whole bunch of new applications.

I don't think so. Again, I think a nicer switch syntax would do zero for 
the success of D.

[snip]

> Finally, you didn't address the suggestion to allow assignment of the
> switch condition to a properly scoped variable: switch([x =] expression) ?
> Some expression results don't have an easily named concept. Performing
> the expression on a prior line and assigning to a temp often results in
> some pretty stupid variable names polluting the outer scope. And it's
> annoying.

That bugzilla is a minor improvement that has precedent in the "if" 
statement, so I'd accept "switch (auto var = expr)" if implemented.


Andrei



More information about the Digitalmars-d mailing list