<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 17 February 2014 03:14, Timon Gehr <span dir="ltr"><<a href="mailto:timon.gehr@gmx.ch" target="_blank">timon.gehr@gmx.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">On 02/16/2014 04:42 PM, Manu wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So D offers great improvements to switch(), but there are a few small<br>
things I wonder about.<br>
<br>
1.<br>
case fall-through is not supported; explicit 'goto case n;' is required.<br>
</blockquote>
<br></div>
Yes it is supported. Use 'goto case;'.</blockquote><div><br></div><div>That's still entirely explicit. It's not fallthrough.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
It could 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>
...<br>
</blockquote>
<br>
Like this: <a href="http://ceylon-lang.org/documentation/reference/statement/switch/" target="_blank">http://ceylon-lang.org/<u></u>documentation/reference/<u></u>statement/switch/</a> ?<br></blockquote><div><br></div><div>
Yes. That's just common sense, right?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
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></div>
...<br>
</blockquote>
<br>
AFAIK it is to emphasize that the range is inclusive, as opposed to:<br>
<br>
case 1..3:<br>
<br>
IIRC Walter's intention was to format it as follows:<br>
<br>
switch(x){<div class=""><br>
case 1:<br>
..<br>
case 3:<br>
}<br></div></blockquote><div><br></div><div>Right. Inclusive ranges are intended for enum ranges though. Integer ranges are just fine in the usual way.</div><div>I'd support both...</div><div><br></div><div><br></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
It's also weird, because it seems that 'case n: .. case m:' is inclusive<br>
of m. This may be unexpected.<br>
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></div>
...<br>
</blockquote>
<br>
Sure, mixing case lists and ranges in a single statement would be neat, but what would be your preferred syntax?<br></blockquote><div><br></div><div>case 1, 2, 5..10, 20:</div><div><br></div><div>But that's not what I actually meant. I was actually talking about the case when 'case 1,2,5:' appears in the same switch() block as 'case 10: .. case 19:', when they appear next to eachother, it just looks awkward; like they don't belong together.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
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></div>
...<br>
</blockquote>
<br>
There is more than one sensible default, so being explicit about the default makes sense.</blockquote><div><br></div><div>Don't agree. Most of my cases are such that no default action is required. It's just noise in my code.</div>
<div>If I fail to handle default when I probably should have, then that's clearly my mistake.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The wasted lines are due to your formatting.<br>
<br>
int difficulty=-1;<div class=""><br>
switch(e.note.note){<br>
case 60: .. case 71: difficulty = 0; break;<br>
case 72: .. case 83: difficulty = 1; break;<br>
case 84: .. case 95: difficulty = 2; break;<br>
case 96: .. case 107: difficulty = 3; break;<br></div>
default: break;<br>
}<br></blockquote><div><br></div><div>I hate this. It violates the formatting conventions used EVERYWHERE else.</div><div>In terms of formatting, it doesn't even look like the same language.</div><div><br></div><div>
<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Of course, I'd just write the above as:<br>
<br>
int difficulty = e.note.note.between(60,108) ? (e.note.note-60)/12 : -1;<br></blockquote><div><br></div><div>Yes yes, very clever. Obviously it's an example and could come in any shape or form.</div><div>Personally, I also wouldn't do that anyway; basic readability has definitely been lost.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
The default case is a total waste, since -1 should just be assigned when<br>
initialising the variable above.<br></div>
...<br>
</blockquote>
<br>
How is the compiler supposed to know? It might be a logic error for e.note.note to be out of range.<br></blockquote><div><br></div><div>Then I should have used an invariant or something actually designed for catching values out of range.</div>
<div>It's not the compilers fault I made a logic error. I could make a logic error literally anywhere in my code. Why force an annoying rule to maybe sometimes catch exactly one case (while making other competing cases where a logic error isn't present more annoying).<br>
</div><div><br></div></div></div></div>