<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div><br>On Dec 23, 2017, at 4:15 PM, Slava Pestov via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 23, 2017, at 3:47 PM, Thomas Roughton via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span class=""></span></div><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">On 24/12/2017, at 9:40 AM, Cheyo Jimenez via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></span></font></div><blockquote type="cite" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class="">What are your thoughts on `final switch` as a way to treat any enum as exhaustible?</span></font><div class=""><a href="https://dlang.org/spec/statement.html#FinalSwitchStatement" style="background-color: rgba(255, 255, 255, 0);" class=""><font class="">https://dlang.org/spec/statement.html#FinalSwitchStatement</font></a></div></blockquote><blockquote type="cite" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">_______________________________________________<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></span></font></blockquote></div><span style="background-color: rgba(255, 255, 255, 0);" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div>I’d be very much in favour of this (qualms about the naming of the ‘final’ keyword aside - ‘complete’ or ‘exhaustive’ reads better to me). </span><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Looking back at the proposal, I noticed that something similar was mentioned that I earlier missed. In the proposal, it says:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><blockquote type="cite" preoffsettop="179" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">However, this results in some of your code being <em style="box-sizing: border-box;" class="">impossible to test,</em> since you can't write a test that passes an unknown value to this switch.<br class=""></span></font></blockquote><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><span style="background-color: rgba(255, 255, 255, 0);" class="">Is that strictly true? Would it be theoretically possible for the compiler to emit or make accessible a special ‘test’ case for non-exhaustive enums that can only be used in test modules or e.g. by a ‘EnumName(testCaseNamed:)’, constructor? There <i class="">is</i> potential for abuse there but it <i class="">would </i>address that particular issue. </span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Regardless, I still feel something like a ‘final switch’ is necessary if this proposal is introduced, and that it fits with the ‘progressive disclosure’ notion; once you learn this keyword you have a means to check for completeness, but people unaware of it could just use a ‘default’ case as per usual and not be concerned with exhaustiveness checking. </span></div></div></div></div></blockquote><br class=""></div><div>My general philosophy with syntax sugar is that it should do more than just remove a constant number of tokens. Basically you’re saying that</div><div><br class=""></div><div>final switch x {}</div><div><br class=""></div><div>just expands to</div><div><br class=""></div><div>switch x { // edited</div><div>default: fatalError()</div><div>}</div><div><br class=""></div><div>I don’t think a language construct like this carries its weight.</div></div></blockquote><div><br></div>Having the ability to treat a non exhaustive enum as if it where exhaustive is not the same as <div><br></div><div>switch x {</div><div>default : fatalError()</div><div>}</div><div><br></div><div>The above will happily let me omit currently compile time known cases. Perhaps ‘final switch’ is not the best but I can’t think of another way to semantically “cast” a non exhaustive as exhaustive. Essentially what I believe we want is a way to treat a non exhaustive as exhaustive during compile time, on the client side. </div><div><br></div><div>It would be cool if we instead repurposed the swift “case _” to handle all compile time known cases and default could then handle all unknown future cases in an non exhaustive enum. </div><div><br></div><div>public enum x {a, b, c, d}</div><div><br></div><div>switch x { // x is non exhaustive here</div><div> case a: print("case a")</div><div> case _ : print(“known cases b c or d”) // sugar for cases b, c, d which are known during compile time. Expanded to mean case b, c, d. </div><div>default: fatalError() // future unknown cases</div><div>}</div><div><br></div><div>I don’t think this would would break any code since all enums have been exhaustive. No new syntax would be added and now there would be a meaningful difference between compile time known cases (case _) vs compile time unknown future cases (default). </div><div><br></div><div><div><br><blockquote type="cite"><div><div><br class=""></div><div>For example, generics have a multiplicative effect on code size — they prevent you from having to write an arbitrary number of versions of the same algorithm for different concrete types.</div><div><br class=""></div><div>Another example is optionals — while optionals don’t necessarily make code shorter, they make it more understandable, and having optionals in the language rules out entire classes of errors at compile time.</div><div><br class=""></div><div>On the other hand, a language feature that just reduces the number of tokens without any second-order effects makes code harder to read, the language harder to learn, and the compiler buggier and harder to maintain without much benefit. So I think for the long term health of the language we should avoid ‘shortcuts’ like this.</div><div><br class=""></div><div>Slava</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></div></body></html>