<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=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 5 janv. 2018 à 09:11, Goffredo Marocchi via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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="">&nbsp; &nbsp;&nbsp;</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="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 14px;" class="">&nbsp;&nbsp; &nbsp;<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="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(129, 53, 160);" class="">private</span>&nbsp;<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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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>) -&gt; <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="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</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="">&nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</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></body></html>