<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Fair concerns Gwendal, but why can’t the default in these cases be just exhaustive / frozen unless the library developer explicitly marks it as “unfrozen/non exhaustive” and the compiler can warn the users when they switch over it instead of throwing an error by default (the user can still treat warnings as errors if they want and suppress this warning if they wanted to in this vision)?<div><br></div><div>I think, sorry if I am being presumptions, that the onus in this case should be on the library author to mark enums and opt in in this non exhaustive behaviour. @Jordan what would the issue in this be?<br><br><div id="AppleMailSignature">Sent from my iPhone</div><div><br>On 5 Jan 2018, at 09:11, Gwendal Roué <<a href="mailto:gwendal.roue@gmail.com">gwendal.roue@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">Le 5 janv. 2018 à 09:11, Goffredo Marocchi via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I feel like this change will make the life easier for library authors, but risks making it easier and easier to make mistakes in apps using the libraries: exhaustive switching over enums is a good important feature that risks going away (the default matters) for not a huge effective benefit to app developers.</div></div></blockquote><br class=""></div><div>I agree that life is made worse for a library user that meets a non-frozen enum. And I don't know if it makes the life easier for library authors, because library authors not only throw code in the wild, but also try to prevent library users from making the mistakes you talk about.</div><div><br class=""></div><div>To take GRDB.swift as an example, it currently has 15 public enums. What are my options?</div><div><br class=""></div><div>More than half of those enums mirror SQLite C enums, or sets of related SQLite values. SQLite is still a living piece of sofware, and it may adds new cases or values in the future. It is then likely that GRDB has to be updated as well. It thus looks like I can make those enums @frozen. But then it looks like I betray the proposal's goal. What's the correct choice?</div><div><br class=""></div><div>Some enums are like SKPaymentTransactionState: there's no way the user could infer a proper behavior in a mandatory default/unknown switch case. I thus also want to make them @frozen, and avoid headaches to the library users. Will I pay for it later?</div><div><br class=""></div><div>Some other enums are positively non-frozen. I may want to replace some of them with a struct with a limited set of pre-built values. This would, IMHO, improve the library UX because it would again prevent headaches for the library users about scary "future" cases. In the struct with a limited set of values, there is no future cases. Instead, there are a limited set of named values. Which is a very different way to say the same thing.</div><div><br class=""></div><div>This last paragraph draws a strong parallel between non-frozen enums and structs:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>// non-frozen enum:</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(129, 53, 160); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>public<span style="color: #000000" class=""> </span>enum<span style="color: #000000" class=""> E {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">case</span> a, b</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 14px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>// equivalent struct:</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(129, 53, 160); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>public<span style="color: #000000" class=""> </span>struct<span style="color: #000000" class=""> S: </span><span style="color: #3c59a7" class="">Equatable</span><span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">private</span> <span style="color: #8135a0" class="">let</span> value: <span style="color: #3c59a7" class="">Int</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: rgb(129, 53, 160);" class="">private</span> <span style="color: rgb(129, 53, 160);" class="">init</span>(<span style="color: rgb(129, 53, 160);" class="">_</span> value: <span style="color: rgb(60, 89, 167);" class="">Int</span>) { <span style="color: rgb(129, 53, 160);" class="">self</span>.<span style="color: rgb(32, 128, 159);" class="">value</span> = value }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">public</span> <span style="color: #8135a0" class="">static</span> <span style="color: #8135a0" class="">let</span> a = <span style="color: #20809f" class="">S</span>(1)</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">public</span> <span style="color: #8135a0" class="">static</span> <span style="color: #8135a0" class="">let</span> b = <span style="color: #20809f" class="">S</span>(2)</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">public</span> <span style="color: #8135a0" class="">static</span> <span style="color: #8135a0" class="">func</span> == (lhs: <span style="color: #20809f" class="">S</span>, rhs: <span style="color: #20809f" class="">S</span>) -> <span style="color: #3c59a7" class="">Bool</span> {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">return</span> lhs.<span style="color: #20809f" class="">value</span> == rhs.<span style="color: #20809f" class="">value</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 12px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">func</span> f(_ s: <span style="color: #20809f" class="">S</span>) {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>// S can be switched over just like a struct:</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">switch</span> s {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">case</span> .<span style="color: #20809f" class="">a</span>: <span style="color: #3c59a7" class="">print</span>(<span style="color: #b4261a" class="">"a"</span>)</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">case</span> .<span style="color: #20809f" class="">b</span>: <span style="color: #3c59a7" class="">print</span>(<span style="color: #b4261a" class="">"b"</span>)</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #8135a0" class="">default</span>: <span style="color: #3c59a7" class="">print</span>(<span style="color: #b4261a" class="">"unknown"</span>)</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div class=""><br class=""></div></div><div>BTW, did anybody think about importing C enums as such structs?</div><div><br class=""></div><div>Gwendal</div><div><br class=""></div></div></blockquote></div></body></html>