<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Is there a reason we cannot use labelled case statements?</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; switch some_value {<br class="">&nbsp; &nbsp; case .REFINED:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; if !validate(some_value) { return NULL }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; fallthrough base</div><div class=""><br class="">&nbsp; &nbsp; base: case .BASE:<br class="">&nbsp; &nbsp; &nbsp; &nbsp; handle_enum_value();<br class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">At least this is explicit now.</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Dec 5, 2015, at 10:04 AM, Vinicius Vendramini &lt;<a href="mailto:vinivendra@gmail.com" class="">vinivendra@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I understand there might be some cases in which the syntax provided is indeed useful for experienced programmers writing their code. However, in almost all the examples here, I had to struggle to understand the logic behind the code. Not because it’s poorly written... probably because this syntax may be used for many different purposes, so it’s hard to get what exactly is the intent behind each use.</div><div class=""><br class=""></div><div class="">In Pierre’s latest example, for instance, it took me a few seconds to understand what was going on. I know it’s a simplified case, but it seems clearer to me to just write something like</div><div class=""><br class=""></div><div class="">if some_value == .Refined &amp;&amp; !validate(some_value) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return NULL</div><div class="">}</div><div class="">handle_enum_value()</div><div class=""><br class=""></div><div class="">More complex cases make for a better argument for `switch`es, mainly because they avoid big `if` pyramids, but especially in those I feel the resulting code is significantly harder to understand.</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 5, 2015, at 12:15 PM, Pierre Habouzit &lt;<a href="mailto:phabouzit@apple.com" class="">phabouzit@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On Dec 5, 2015, at 9:02 AM, Chris Lattner &lt;<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>&gt; wrote:<br class=""><br class="">On Dec 4, 2015, at 2:05 PM, <a href="mailto:jalkut@red-sweater.com" class="">jalkut@red-sweater.com</a> wrote:<br class=""><blockquote type="cite" class="">In the spirit of some other proposals that remove C or C++ style artifacts, what do folks think about the possibility of removing the "fallthrough" keyword from the language?<br class=""></blockquote><br class="">I’m not making an argument either way, but I want to point something out: there is a major difference between fallthrough vs ++/c-style-for. &nbsp;To someone who doesn’t know them, the later are "syntactic magic” with no reasonable way to&nbsp;decipher other than looking them up somewhere. &nbsp;The former is an English word whose meaning is obvious in context.<br class=""><br class="">All I’m saying is that to a reader of code, the “badness” of ++ and c-style for loops is greater than the “badness" of fallthrough.<br class=""></blockquote><br class=""><div class="">Given that Swift has the goal to also be a low level language, fallthrough is really useful for conciseness and readability.</div><div class=""><br class=""></div><div class="">in system programming C, I find myself writing things like this very often:</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">switch (some_value) {</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">case ENUM_VALUE_REFINED:</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; if (validate(some_value)) {</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; return NULL;</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; }</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; /* fallthrough */</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">case ENUM_VALUE_BASE:</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; handle_enum_value();</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;…</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">}</font></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class=""></blockquote>Where the swift equivalent would roughly be:<br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><br class=""></div><div class=""><font face="Menlo" class="">switch some_value {</font></div><div class=""><font face="Menlo" class="">case .REFINED:</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; if !validate(some_value) { return NULL }</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; fallthrough</font></div><div class=""><font face="Menlo" class="">case .BASE:</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; handle_enum_value();</font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><br class=""><div class="">This is as readable as it gets and is a pattern that the libdispatch has in several places e.g.</div><div class=""><br class=""></div><div class="">Of course, you cannot fall through to arbitrary cases, so things like this C code cannot be done in swift:</div><div class=""><br class=""></div><div class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">switch (some_value) {</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">case ENUM_VALUE_REFINED_1:</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp; if (validate(some_value)) {</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; return NULL;</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp;</font><span style="font-family: Menlo;" class="">&nbsp; &nbsp;}</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp; goto base_value;</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">case ENUM_VALUE_REFINED_2:</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp; if (validate(some_value)) {</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; return NULL;</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp;</font><span style="font-family: Menlo;" class="">&nbsp; &nbsp;}</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp; goto base_value;</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class=""><br class=""></span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class="">case ENUM_VALUE_BASE:</span></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">base_value:</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp; handle_enum_value();</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;…</font></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><font face="Menlo" class="">}</font></blockquote></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><br class=""></div><div class="">cannot be written in swift, despite also being quite useful.</div><div class=""><br class=""></div><div class="">Jumping between arbitrary points inside a switch is disgusting. jumping from label to label is useful and not harmful especially in swift where you can’t place code between the “switch” and the first case.</div><div class=""><br class=""></div><div class="">-Pierre</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=LFwSuj9FMv8p5lHRk78JpoTG6TdqPOb6ZXyNv3YIC3zVDOh8SFmEynnCFkZSU96i9QOrOOTyNxaypltLlSJqLIZdWsED-2BOMeJdGYkJjC7ncF2Xa2dkhlZN0W2Ze0g-2BfyoGT9FF6mkqHPUUfRquNLw79AGIbmVY0a3LpUa2jsib6wiFWIzHuPN7I6CZVx0FEp6-2FnUBsFTulUc6c1bw4PUnm6wkUbE8SeHhxQCkvAyzK8-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43pXkLx-2B36P-2BPNJufHeY0dgfEnSrTiflGiTqLfYLhLg624kToIkI0RhrFjsk0TyglfEmpl9ujuhls2gfK5hl8AeRlCjJ-2BoFHUvE8L8GnwiICsBRMYm224aw6wlGWfoEMwIxTmok3Eu2lY32Pz-2BfgmhDQtrJ2-2BJIkkH4ku1XDuJewD869cNDDaUp6bsK1hIti0MQ-3D-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>