<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="">I just realized that I didn't reply to that.<div class=""><br class=""></div><div class="">This works with some caveats:</div><div class=""><br class=""></div><div class=""><ul class="MailOutline"><li class="">you need to maintain a manual mapping of error codes to integer values (good thing that I didn't actually have 300 cases between PermissionDenied and FluxCapacitorFailure);</li><li class="">you now need an UnknownError to satisfy that IntegerLiteralConvertible's constructor is not failable (though in a real-world scenario you might need one anyway);</li><li class="">you can't convert automatically between FileSystemError and MyLibError (leading to code less terse than it could).</li></ul><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 18 déc. 2015 à 13:42:39, T.J. Usiyan <<a href="mailto:griotspeak@gmail.com" class="">griotspeak@gmail.com</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I think that you can accomplish this right now if you make your backing enum literal convertible. String literal would have been the better choice in the example below but I was feeling lazy.<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="color:rgb(187,44,162)" class="">public</span><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">enum</span><span style="" class=""> MyLibError: </span>ErrorType<span style="" class="">, </span>IntegerLiteralConvertible<span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> FileNotFound</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> UnexpectedEOF</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> PermissionDenied</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>// ... 300 cases later</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> FluxCapacitorFailure</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> SplineReticulationError</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> UnknownError</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">init</span>(integerLiteral value: <span style="color:rgb(112,61,170)" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">switch</span> value {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> <span style="color:rgb(39,42,216)" class="">0</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">self</span> = .<span style="color:rgb(49,89,93)" class="">FileNotFound</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> <span style="color:rgb(39,42,216)" class="">1</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">self</span> = .<span style="color:rgb(49,89,93)" class="">UnexpectedEOF</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> <span style="color:rgb(39,42,216)" class="">2</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">self</span> = .<span style="color:rgb(49,89,93)" class="">PermissionDenied</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> <span style="color:rgb(39,42,216)" class="">3</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">self</span><span style="" class=""> = .</span>FluxCapacitorFailure</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> <span style="color:rgb(39,42,216)" class="">4</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">self</span><span style="" class=""> = .</span>SplineReticulationError</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">default</span>:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">self</span> = .<span style="color:rgb(49,89,93)" class="">UnknownError</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">enum</span> FileSystemError: <span style="color:rgb(79,129,135)" class="">MyLibError</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> FileNotFound = <span style="color:rgb(39,42,216)" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> UnexpectedEOF = <span style="color:rgb(39,42,216)" class="">1</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> PermissionDenied = <span style="color:rgb(39,42,216)" class="">2</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Dec 18, 2015 at 12:34 PM, Dennis Lysenko via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="">Sorry, I got a bit too excited and skimmed over the most important part of the idea. So this is a special type of enum declaration in which you cannot declare any new enum members. I personally have not seen a use for this in my code but I would love to hear others' response to it. <span style="line-height:1.5" class="">It is a very interesting idea though.</span></div><div class=""><span style="line-height:1.5" class=""><br class=""></span></div><div class=""><span style="line-height:1.5" class="">I'm going to go out on a limb with an idea that is in the same vein as this one: What if we favored composition over inheritance here, and made it so that you could transparently refer to members of other enums *without* having another enum as a backing type?</span><br class=""></div><div class=""><br class=""></div><div class="">e.g., you have:</div><div class="">enum NetworkException {</div><div class=""> case NoInternetError, SecurityError</div><div class="">}</div><div class=""><br class=""></div><div class="">enum ParseException {</div><div class=""> case FailedResponse(statusCode: Int)</div><div class=""> case EmptyResponse</div><div class=""> case MissingField(fieldName: String)</div><div class="">}</div><div class=""><br class=""></div><div class="">As two general classes of errors. But for a full API call wrapper, you might want an error class that composes the two, so that when calling the API call from your UI code, you can display a "please check your connection" message for NoInternetError, a "Please log in" error for FailedResponse with statusCode=401, or a "server error" message for any of the rest. </div><div class=""><br class=""></div><div class="">I wonder how do you and others feel about that use-case? I have certainly seen it come up a lot in real-world projects that require resilient UI interactions with nontrivial networking operations.</div><div class=""><br class=""></div><div class="">Here are some quick code samples off the top of my head for how we might go about this (let's say the API operation is "change profile picture":</div><div class=""><br class=""></div><div class="">enum ChangePictureError {</div><div class=""> include NetworkException</div><div class=""> include ParseException</div><div class=""> case PictureTooLarge</div><div class="">}</div><div class=""><br class=""></div><div class="">or</div><div class=""><br class=""></div><div class="">enum ChangePictureError {</div><div class=""> compose NetworkException.NoInternetError</div><div class=""> compose ParseException.EmptyResponse</div><div class=""> compose ParseException.FailedResponse(statusCode: Int)</div><div class=""> case PictureTooLarge</div><div class="">}</div><div class=""><br class=""></div><div class="">Not a proposal by any stretch of the imagination, just a potential direction inspired by your idea, Felix.</div><div class=""><br class=""></div></div><div class="HOEnZb"><div class="h5"><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Dec 18, 2015 at 12:21 PM Dennis Lysenko <<a href="mailto:dennis.s.lysenko@gmail.com" target="_blank" class="">dennis.s.lysenko@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">Felix, <div class=""><br class=""></div><div class="">This seems to be very interestingly tied into your comments about polymorphism in 'throws' type annotations. Would you not feel that allowing enums to be built on top of other enums would promote the kind of egregious proliferation of exception polymorphism that discourages so many from following Java's checked exception model? </div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Dec 18, 2015 at 11:29 AM Félix Cloutier <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br class="">
<br class="">
Swift currently has more or less three conceptual types of enums: discriminated unions, lists of unique tokens, and lists of value of a raw type.<br class="">
<br class="">
> // Discriminated unions<br class="">
> enum Foo {<br class="">
> case Bar(Int)<br class="">
> case Baz(String)<br class="">
> }<br class="">
><br class="">
> // Lists of unique tokens (mixable with discriminated unions)<br class="">
> enum Foo {<br class="">
> case Frob<br class="">
> case Nicate<br class="">
> }<br class="">
><br class="">
> // Lists of raw values<br class="">
> enum Foo: String {<br class="">
> case Bar = "Bar"<br class="">
> case Baz = "Baz"<br class="">
> }<br class="">
<br class="">
I think that the last case could be made more interesting if you could use more types as underlying types. For instance, it could probably be extended to support another enum as the backing type. One possible use case would be to have a big fat enum for all the possible errors that your program/library can throw, but refine that list into a shorter enum for functions that don't need it all.<br class="">
<br class="">
> enum MyLibError: ErrorType {<br class="">
> case FileNotFound<br class="">
> case UnexpectedEOF<br class="">
> case PermissionDenied<br class="">
> // ... 300 cases later<br class="">
> case FluxCapacitorFailure<br class="">
> case SplineReticulationError<br class="">
> }<br class="">
><br class="">
> enum FileSystemError: MyLibError {<br class="">
> case FileNotFound = .FileNotFound<br class="">
> case UnexpectedEOF = .UnexpectedEOF<br class="">
> case PermissionDenied = .PermissionDenied<br class="">
> }<br class="">
<br class="">
This example could be made simpler if the `= .Foo` part was inferred from the name, but you get the idea.<br class="">
<br class="">
In this case, it would be helpful (but not required) that FileSystemError was convertible into a MyLibError, so that it could be transparently rethrown in a function that uses the larger enum. I personally don't see why enums with a specified underlying type can't be implicitly converted to it, but this is not currently the case and it probably deserves some discussion as well.<br class="">
<br class="">
Is there any interest in that?<br class="">
<br class="">
Félix<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div></blockquote></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCrTj2WYe4yzpVmMCGJeOAT-2BFk2N2-2BbHLTaaMr-2Fn7gD1qg0TkI3DCoprLwtkva-2Byfft8WJ4hjCjgu8EDYPZxSKSJkIAx7u97RrEWZ69-2FD9g1L4LgwXry9VcEiVu5cs0To2fvauFVwHwk7PHP6wzF-2FqEx-2FfSmOoz0f50v8SyndBmEIxrNoXecfvmyxwdeoPaE8zo-3D" alt="" width="1" height="1" border="0" style="min-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></div><br 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" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>