<div dir="ltr">Another thing that I often do is switch on an expression, but I often have a problem where, within the scope, I have no way to refer to the result value.<div>This is particularly important when range cases appear.<div>
<br></div><div>switch(x+10)</div><div>{</div><div>  case 10: .. case 20:</div><div>    // what is the value?</div><div>}<br></div><div><br></div><div>Obviously then I need to do this:</div><div><br></div><div><div>auto y = x+10;</div>
<div>switch(y)</div><div>{</div><div>  case 10: .. case 20:</div><div>    // I know y...</div><div>}<br></div></div><div><br></div><div>But this is a bit lame. I'm polluting the outer namespace, and wasting a line.</div>
<div><br></div><div>I wonder if a variable declaration could be made possible in the switch:</div><div><br></div><div>switch(y = x+10) // obviously implicitly auto, like in foreach</div><div>{</div><div>  case 10: .. case 20:</div>
<div>    // I have y, no pollution of the outer scope, no wasted line. yay!</div><div>}</div><div><br></div><div>for and foreach can both declare variables this way... does it make sense here? Useful?<br><br></div></div></div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On 17 February 2014 01:42, Manu <span dir="ltr"><<a href="mailto:turkeyman@gmail.com" target="_blank">turkeyman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">So D offers great improvements to switch(), but there are a few small things I wonder about.<div><br></div><div>1.</div><div>case fall-through is not supported; explicit 'goto case n;' is required.</div>

<div>With this in mind, 'break' is unnecessary. Why is it required? It could be implicit upon reaching the next case label, or a scope could be used (with support for omitting the scope for single statements as with if).</div>

<div>It's really noisy, and annoying to write everywhere.</div><div><br></div><div>2.</div><div>'case 1, 3, 7, 8:' is awesome! ...but ranged cases have a totally different syntax: 'case 1: .. case 3:'</div>

<div><br></div><div>Why settle on that syntax? The inconsistency looks kinda silly when they appear together in code.</div><div>Surely it's possible to find a syntax that works without repeating case and ':'?</div>

<div><br></div><div>It's also weird, because it seems that 'case n: .. case m:' is inclusive of m. This may be unexpected.</div><div>I'm not sure it's reasonable to use the '..' syntax in this case for that reason. '..' is an [) range, case ranges must be [] so that it makes sense when dealing with enum key ranges.</div>

<div><br></div><div>3.</div><div>Why is 'default' necessary? If I'm not switching on an enumerated type, then many values are meaningless. requiring an empty 'default: break;' line at the end is annoying and noisy.</div>

<div><br></div><div>I often find myself tempted to rewrite blocks of successive if() logic comparing integers against values/ranges, but it looks silly since the scope rules are not explicit, and 'default: break;' always wastes an extra line.</div>

<div>I like to reduce noise in my code, and these switch semantics threaten to simplify a lot of code, if not for these strange decisions (purely for legacy compliance?).</div><div><br></div><div><br></div><div>Let's consider an example:</div>

<div><br></div><div><div>Code like this:</div><div><br></div><div>int difficulty = -1;</div><div>if(e.note.note >= 60 && e.note.note < 72)</div><div><span style="white-space:pre-wrap">      </span>difficulty = 0;</div>

<div>else if(e.note.note >= 72 && e.note.note < 84)</div><div><span style="white-space:pre-wrap">       </span>difficulty = 1;</div><div>else if(e.note.note >= 84 && e.note.note < 96)</div><div><span style="white-space:pre-wrap">       </span>difficulty = 2;</div>

<div>else if(e.note.note >= 96 && e.note.note < 108)</div><div><span style="white-space:pre-wrap">      </span>difficulty = 3;</div><div><br></div><div>The repetition of e.note.note is annoying, and particular choice of comparisons are liable to result in out-by-ones. It's not nice code to read.</div>

<div><br></div><div><br></div><div>Rewrites like this:</div><div><br></div><div>int difficulty;</div><div>switch(e.note.note)</div><div>{</div><div><span style="white-space:pre-wrap">     </span>case 60: .. case 71:</div>
<div><span style="white-space:pre-wrap">          </span>difficulty = 0;</div><div><span style="white-space:pre-wrap">          </span>break;</div><div><span style="white-space:pre-wrap">   </span>case 72: .. case 83:</div><div>
<span style="white-space:pre-wrap">             </span>difficulty = 1;</div><div><span style="white-space:pre-wrap">          </span>break;</div><div><span style="white-space:pre-wrap">   </span>case 84: .. case 95:</div><div><span style="white-space:pre-wrap">             </span>difficulty = 2;</div>

<div><span style="white-space:pre-wrap">          </span>break;</div><div><span style="white-space:pre-wrap">   </span>case 96: .. case 107:</div><div><span style="white-space:pre-wrap">            </span>difficulty = 3;</div><div>
<span style="white-space:pre-wrap">             </span>break;</div><div><span style="white-space:pre-wrap">   </span>default:</div><div><span style="white-space:pre-wrap">         </span>difficulty = -1;</div><div><span style="white-space:pre-wrap">         </span>break;</div>

<div>}</div><div><br></div><div>That's horrid, it's much longer! And there are pointless wasted lines everywhere.</div><div>The default case is a total waste, since -1 should just be assigned when initialising the variable above.</div>

<div><br></div><div><br></div><div>We can compact it a bit like this:</div><div><br></div><div>int difficulty;</div><div>switch(e.note.note)</div><div>{</div><div><span style="white-space:pre-wrap">      </span>case 60: .. case 71:</div>

<div><span style="white-space:pre-wrap">          </span>difficulty = 0; break;</div><div><span style="white-space:pre-wrap">   </span>case 72: .. case 83:</div><div><span style="white-space:pre-wrap">             </span>difficulty = 1; break;</div>

<div><span style="white-space:pre-wrap">  </span>case 84: .. case 95:</div><div><span style="white-space:pre-wrap">             </span>difficulty = 2; break;</div><div><span style="white-space:pre-wrap">   </span>case 96: .. case 107:</div>

<div><span style="white-space:pre-wrap">          </span>difficulty = 3; break;</div><div><span style="white-space:pre-wrap">   </span>default:</div><div><span style="white-space:pre-wrap">         </span>difficulty = -1; break;</div>
<div>}</div><div><br></div><div>But that's horrible too. It's not clear what vertical offset the 'break' statements shoudl appear at (I hate stacking up multiple statements across the same line!).</div><div>

The the default case is still a waste.</div><div><br></div><div><br></div><div>Ideally:</div><div><br></div><div>int difficulty = -1;</div><div>switch(e.note.note)</div><div>{</div><div><span style="white-space:pre-wrap"> </span>case 60 .. 72:</div>

<div><span style="white-space:pre-wrap">          </span>difficulty = 0;</div><div><span style="white-space:pre-wrap">  </span>case 72 .. 84:</div><div><span style="white-space:pre-wrap">           </span>difficulty = 1;</div><div>
<span style="white-space:pre-wrap">     </span>case 84 .. 96:</div><div><span style="white-space:pre-wrap">           </span>difficulty = 2;</div><div><span style="white-space:pre-wrap">  </span>case 96 .. 108:</div><div><span style="white-space:pre-wrap">          </span>difficulty = 3;</div>

<div>}</div><div><br></div><div>'break's are unnecessary since fallthrough isn't allowed.</div><div>Proper numeric range could be supported (assuming [) intervals).</div><div>'default' case is unnecessary, and removed.</div>

<div><br></div><div>The switch and braces results in 3 extra lines, but I still feel this level of simplification results in code that is MUCH more readable than the sequence of if's I started with. It's super obvious what's going on.<br>

</div><div><br></div><div>I have quite many blocks like this.</div></div><div><br></div><div>A great man once (actually, frequently) said "If it doesn't look right, it probably isn't".</div></div>
</blockquote></div><br></div>