<div dir="ltr"><div>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&#39; response to it. <span style="line-height:1.5">It is a very interesting idea though.</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">I&#39;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></div><div><br></div><div>e.g., you have:</div><div>enum NetworkException {</div><div>  case NoInternetError, SecurityError</div><div>}</div><div><br></div><div>enum ParseException {</div><div>  case FailedResponse(statusCode: Int)</div><div>  case EmptyResponse</div><div>  case MissingField(fieldName: String)</div><div>}</div><div><br></div><div>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 &quot;please check your connection&quot; message for NoInternetError, a &quot;Please log in&quot; error for FailedResponse with statusCode=401, or a &quot;server error&quot; message for any of the rest. </div><div><br></div><div>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><br></div><div>Here are some quick code samples off the top of my head for how we might go about this (let&#39;s say the API operation is &quot;change profile picture&quot;:</div><div><br></div><div>enum ChangePictureError {</div><div>  include NetworkException</div><div>  include ParseException</div><div>  case PictureTooLarge</div><div>}</div><div><br></div><div>or</div><div><br></div><div>enum ChangePictureError {</div><div>  compose NetworkException.NoInternetError</div><div>  compose ParseException.EmptyResponse</div><div>  compose ParseException.FailedResponse(statusCode: Int)</div><div>  case PictureTooLarge</div><div>}</div><div><br></div><div>Not a proposal by any stretch of the imagination, just a potential direction inspired by your idea, Felix.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 18, 2015 at 12:21 PM Dennis Lysenko &lt;<a href="mailto:dennis.s.lysenko@gmail.com">dennis.s.lysenko@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Felix, <div><br></div><div>This seems to be very interestingly tied into your comments about polymorphism in &#39;throws&#39; 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&#39;s checked exception model? </div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 18, 2015 at 11:29 AM Félix Cloutier &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
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>
<br>
&gt; // Discriminated unions<br>
&gt; enum Foo {<br>
&gt;       case Bar(Int)<br>
&gt;       case Baz(String)<br>
&gt; }<br>
&gt;<br>
&gt; // Lists of unique tokens (mixable with discriminated unions)<br>
&gt; enum Foo {<br>
&gt;       case Frob<br>
&gt;       case Nicate<br>
&gt; }<br>
&gt;<br>
&gt; // Lists of raw values<br>
&gt; enum Foo: String {<br>
&gt;       case Bar = &quot;Bar&quot;<br>
&gt;       case Baz = &quot;Baz&quot;<br>
&gt; }<br>
<br>
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&#39;t need it all.<br>
<br>
&gt; enum MyLibError: ErrorType {<br>
&gt;       case FileNotFound<br>
&gt;       case UnexpectedEOF<br>
&gt;       case PermissionDenied<br>
&gt;       // ... 300 cases later<br>
&gt;       case FluxCapacitorFailure<br>
&gt;       case SplineReticulationError<br>
&gt; }<br>
&gt;<br>
&gt; enum FileSystemError: MyLibError {<br>
&gt;       case FileNotFound = .FileNotFound<br>
&gt;       case UnexpectedEOF = .UnexpectedEOF<br>
&gt;       case PermissionDenied = .PermissionDenied<br>
&gt; }<br>
<br>
This example could be made simpler if the `= .Foo` part was inferred from the name, but you get the idea.<br>
<br>
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&#39;t see why enums with a specified underlying type can&#39;t be implicitly converted to it, but this is not currently the case and it probably deserves some discussion as well.<br>
<br>
Is there any interest in that?<br>
<br>
Félix<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></blockquote></div>