<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 class="" style="white-space:pre">        </span>difficulty = 0;</div>
<div>else if(e.note.note >= 72 && e.note.note < 84)</div><div><span class="" style="white-space:pre"> </span>difficulty = 1;</div><div>else if(e.note.note >= 84 && e.note.note < 96)</div><div><span class="" style="white-space:pre"> </span>difficulty = 2;</div>
<div>else if(e.note.note >= 96 && e.note.note < 108)</div><div><span class="" style="white-space:pre">        </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 class="" style="white-space:pre">       </span>case 60: .. case 71:</div>
<div><span class="" style="white-space:pre">            </span>difficulty = 0;</div><div><span class="" style="white-space:pre">            </span>break;</div><div><span class="" style="white-space:pre">     </span>case 72: .. case 83:</div><div>
<span class="" style="white-space:pre">               </span>difficulty = 1;</div><div><span class="" style="white-space:pre">            </span>break;</div><div><span class="" style="white-space:pre">     </span>case 84: .. case 95:</div><div><span class="" style="white-space:pre">               </span>difficulty = 2;</div>
<div><span class="" style="white-space:pre">            </span>break;</div><div><span class="" style="white-space:pre">     </span>case 96: .. case 107:</div><div><span class="" style="white-space:pre">              </span>difficulty = 3;</div><div>
<span class="" style="white-space:pre">               </span>break;</div><div><span class="" style="white-space:pre">     </span>default:</div><div><span class="" style="white-space:pre">           </span>difficulty = -1;</div><div><span class="" style="white-space:pre">           </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 class="" style="white-space:pre">        </span>case 60: .. case 71:</div>
<div><span class="" style="white-space:pre">            </span>difficulty = 0; break;</div><div><span class="" style="white-space:pre">     </span>case 72: .. case 83:</div><div><span class="" style="white-space:pre">               </span>difficulty = 1; break;</div>
<div><span class="" style="white-space:pre">    </span>case 84: .. case 95:</div><div><span class="" style="white-space:pre">               </span>difficulty = 2; break;</div><div><span class="" style="white-space:pre">     </span>case 96: .. case 107:</div>
<div><span class="" style="white-space:pre">            </span>difficulty = 3; break;</div><div><span class="" style="white-space:pre">     </span>default:</div><div><span class="" style="white-space:pre">           </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 class="" style="white-space:pre">   </span>case 60 .. 72:</div>
<div><span class="" style="white-space:pre">            </span>difficulty = 0;</div><div><span class="" style="white-space:pre">    </span>case 72 .. 84:</div><div><span class="" style="white-space:pre">             </span>difficulty = 1;</div><div>
<span class="" style="white-space:pre">       </span>case 84 .. 96:</div><div><span class="" style="white-space:pre">             </span>difficulty = 2;</div><div><span class="" style="white-space:pre">    </span>case 96 .. 108:</div><div><span class="" style="white-space:pre">            </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>