<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 17 February 2014 16:18, Andrei Alexandrescu <span dir="ltr"><<a href="mailto:SeeWebsiteForEmail@erdani.org" target="_blank">SeeWebsiteForEmail@erdani.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">On 2/16/14, 7:42 AM, Manu wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
So D offers great improvements to switch(), but there are a few small<br>
things I wonder about.<br>
</blockquote>
<br></div>
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.<br>
</blockquote><div><br></div><div>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.</div><div><br>
</div><div>So, you admit that this is a disaster site, but are reluctant to consider what good may come of it?</div><div>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.</div>
<div><br></div><div>There are like, 5 control statements in the language; if, for, while, foreach, switch. To trivialise their importance is surprising.<br></div><div>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.</div>
<div>And it is shit, it's barely better than C; it's verbose, nobody knows how to format 'case' statements, it always seems to violate formatting standards whenever it's used (disagrees with all code everywhere else), programmers always seem to disagree on how it should be.</div>
<div>In my opinionated opinion, it always seems to look ugly, and unnecessarily verbose.</div><div><br></div><div>The ugliness of the syntax makes it an unattractive choice for application to many problems where it may have otherwise been applicable, simplifying code, and making the code more readable and understandable at a glance.<br>
</div><div>The switch statement was obviously initially designed to improve clarity; anything that can be done with a switch statement can easily be done with a series of if's (if you love squinting at comparative conditions and constant repetition of the terms being compared), and it probably did improve clarity, in 1970's...</div>
<div>But I think we could do much better, and offer an interesting and useful construct that could be used to clarify code in far more locations than it ever sees use today.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
1.<br>
case fall-through is not supported; explicit 'goto case n;' is required.<br></div>
With this in mind, 'break' is unnecessary. Why is it required? It could<div class=""><br>
be implicit upon reaching the next case label, or a scope could be used<br>
(with support for omitting the scope for single statements as with if).<br>
It's really noisy, and annoying to write everywhere.<br>
</div></blockquote>
<br>
Implicit fall-through has been specifically eliminated from the language at my behest. I think it is a fine language change. Use "goto case;" to clarify to the maintainer (and incidentally the compiler) you want a fall-through.</blockquote>
<div><br></div><div>It's a patch on a broken design. If switch used proper scoping rules like everything else, nobody would have ever thought that was a reasonable idea.<br></div><div><br></div><div><div>To be clear, perhaps I didn't explain myself clearly; I'm not arguing with the decision about fall-through. I agree that fall-through should be an explicit request.</div>
<div>I'm suggesting that in the context of D, where fallthrough is not supported (I offer you congratulations for getting that one in), then 'break;' is entirely pointless, and shouldn't be required.</div>
<div><br></div><div>I understand the counter argument to be that "if I paste C code into my D code, there's a potential fail!".</div><div>And my response is, I think it's lame to gimp D because a language designed 45 years ago had a crappy design.</div>
<div>The solution is probably to leave switch as is, and introduce a replacement that's better.</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
2.<br>
'case 1, 3, 7, 8:' is awesome! ...but ranged cases have a totally<br>
different syntax: 'case 1: .. case 3:'<br>
<br>
Why settle on that syntax? The inconsistency looks kinda silly when they<br>
appear together in code.<br>
Surely it's possible to find a syntax that works without repeating case<br>
and ':'?<br>
</blockquote>
<br></div>
There's no inconsistency. The case is not identical with "foreach (a .. b)" or arr[a .. b], both of which don't include b in the explored range. The fact that "case b:" is present is very telling "b" will be acted upon.</blockquote>
<div><br></div><div>I never suggested a..b used in a switch should be inclusive. I was just commenting on the choice to use '..' at all.</div><div>I have said elsewhere that I agree '..' should indeed be exclusive, just like everywhere else.</div>
<div>I think the existing syntax is useful for enums, and could remain. Perhaps both should be supported?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
It's also weird, because it seems that 'case n: .. case m:' is inclusive<br>
of m. This may be unexpected.<br>
</blockquote>
<br></div>
It's expected.</blockquote><div><br></div><div>How so? It seems logical in the context of case statements, but '..' does something different in every other instance. If I were new to D, and I saw that, I imagine it would be perfectly reasonable for me to be confused.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
I'm not sure it's reasonable to use the '..' syntax in this case for<br>
that reason. '..' is an [) range, case ranges must be [] so that it<br>
makes sense when dealing with enum key ranges.<br>
</blockquote>
<br></div>
No.</blockquote><div><br></div><div>No what?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
3.<br>
Why is 'default' necessary? If I'm not switching on an enumerated type,<br>
then many values are meaningless. requiring an empty 'default: break;'<br>
line at the end is annoying and noisy.<br>
</blockquote>
<br></div>
Explicit is better than implicit.</blockquote><div><br></div><div>No.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
I often find myself tempted to rewrite blocks of successive if() logic<br>
comparing integers against values/ranges, but it looks silly since the<br>
scope rules are not explicit, and 'default: break;' always wastes an<br>
extra line.<br>
</blockquote>
<br></div>
Write the line.<br></blockquote><div><br></div><div>No, it looks stupid. If there are few enough cases, I'll use if's instead every time.</div><div>I lose, and the language loses too.</div><div><br></div><div><br>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
I like to reduce noise in my code, and these switch semantics threaten<br></div>
to simplify a lot of code, if not for these strange decisions (purely<br>
for legacy compliance?).<br>
</blockquote>
<br>
I think the current switch statement design is a fine design all things considered (compatibility, law of least surprise, usability, readability).</blockquote><div><br></div><div>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).</div>
<div><br></div><div>Compatibility and law of least surprise I'll grant you.</div><div>Usability and readability, I don't think so. Certainly not compared to competition.</div><div><br></div><div>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.</div>
<div><br></div><div>I concede that switch can't be fixed. Perhaps it should be deprecated instead.</div><div>So I guess on that note, I'll leave it there, but I still think it's worth consideration.</div><div>
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.</div><div>I think you'd see a quick erosion of if/else if sequences if switch was more awesome.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Let's consider an example:<br>
<br>
Code like this:<br>
<br>
int difficulty = -1;<br>
if(e.note.note >= 60 && e.note.note < 72)<br>
difficulty = 0;<br>
else if(e.note.note >= 72 && e.note.note < 84)<br>
difficulty = 1;<br>
else if(e.note.note >= 84 && e.note.note < 96)<br>
difficulty = 2;<br>
else if(e.note.note >= 96 && e.note.note < 108)<br>
difficulty = 3;<br>
<br>
The repetition of e.note.note is annoying, and particular choice of<br>
comparisons are liable to result in out-by-ones. It's not nice code to read.<br>
</blockquote>
<br></div>
For every proposed tweak there will be an example that makes it look great.<br></blockquote><div><br></div><div>I see lots and lots of these; opportunities for a structure like an advanced switch to make really nice.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
Rewrites like this:<br>
<br>
int difficulty;<br></div><div class="">
switch(e.note.note)<br>
{<br>
case 60: .. case 71:<br>
difficulty = 0;<br>
break;<br>
case 72: .. case 83:<br>
difficulty = 1;<br>
break;<br>
case 84: .. case 95:<br>
difficulty = 2;<br>
break;<br>
case 96: .. case 107:<br>
difficulty = 3;<br>
break;<br>
default:<br></div><div class="">
difficulty = -1;<br>
break;<br>
}<br>
<br>
That's horrid, it's much longer! And there are pointless wasted lines<br>
everywhere.<br></div><div class="">
The default case is a total waste, since -1 should just be assigned when<br>
initialising the variable above.<br>
</div></blockquote>
<br>
But you'd be wasting an extra assignment. I recall you're one for efficiency.<br></blockquote><div><br></div><div>I've never encountered an optimiser that would fail on something so simple. Even on my Amiga.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
We can compact it a bit like this:<br>
<br>
int difficulty;<br></div><div class="">
switch(e.note.note)<br>
{<br>
case 60: .. case 71:<br>
difficulty = 0; break;<br>
case 72: .. case 83:<br>
difficulty = 1; break;<br>
case 84: .. case 95:<br>
difficulty = 2; break;<br>
case 96: .. case 107:<br>
difficulty = 3; break;<br>
default:<br></div><div class="">
difficulty = -1; break;<br>
}<br>
<br>
But that's horrible too.<br>
</div></blockquote>
<br>
The quality of being horrible is in the eye of the beholder. I find this code entirely reasonable.</blockquote><div><br></div><div>Really?</div><div>Can you point me to the paragraph in the formatting/style guide that talks about appropriate styling for writing multiple statements on the same line?</div>
<div>What happened to DRY?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
It's not clear what vertical offset the 'break'<br>
statements shoudl appear at (I hate stacking up multiple statements<br>
across the same line!).<br>
The the default case is still a waste.<br>
</blockquote>
<br></div>
Just do it.</blockquote><div><br></div><div>Nope, I'll use if instead.</div><div>DRY and all that. I'd rather repeat the term being used for comparison than repeat 'case' and 'break' all over the place.</div>
<div>'if' produces a similar amount of repetition (perhaps less), but results in properly scoped statements, and I know how it should be formatted.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Ideally:<br>
<br>
int difficulty = -1;<br>
switch(e.note.note)<br>
{<br>
case 60 .. 72:<br>
difficulty = 0;<br>
case 72 .. 84:<br>
difficulty = 1;<br>
case 84 .. 96:<br>
difficulty = 2;<br>
case 96 .. 108:<br>
difficulty = 3;<br>
}<br>
</blockquote>
<br></div>
Nope.</blockquote><div><br></div><div>Elaborate?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
'break's are unnecessary since fallthrough isn't allowed.<br>
</blockquote>
<br></div>
Silently change the semantics of C code is not something we entertain doing.</blockquote><div><br></div><div>I agree, we'd need to distinguish the fixed version.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Proper numeric range could be supported (assuming [) intervals).<br>
'default' case is unnecessary, and removed.<br>
<br>
The switch and braces results in 3 extra lines, but I still feel this<br>
level of simplification results in code that is MUCH more readable than<br>
the sequence of if's I started with. It's super obvious what's going on.<br>
<br>
I have quite many blocks like this.<br>
<br>
A great man once (actually, frequently) said "If it doesn't look right,<br>
it probably isn't".<br>
</blockquote>
<br></div>
The thing is you can apply that to everything, and justify every tweak, because what looks right is subjective.</blockquote><div><br></div><div>Are you saying that switch looks pleasing and coherent to you? As compared with all the other flow control statements existing in D, and competitive statements from other languages?</div>
<div>Should I start a poll? Perhaps science can tell us if people find it to be regular and pleasant syntactically.</div><div><br></div><div><br></div><div>Finally, you didn't address the suggestion to allow assignment of the switch condition to a properly scoped variable: switch([x =] expression) ?</div>
<div>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.</div>
</div></div></div>