<p dir="ltr">Felix, </p>
<p dir="ltr">I cannot speak for anyone but myself, but my idea was that the "included" cases would be treated as a member--whether first or second class, still a member--of the enum including them. I wholeheartedly agree that the example you wrote up would be undesirable. </p>
<br><div class="gmail_quote"><div dir="ltr">On Sat, Dec 19, 2015, 4:36 PM Félix Cloutier <<a href="mailto:felixcca@yahoo.ca">felixcca@yahoo.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I'm biased as the pitcher, but I find that an "inheritance" model would be more straightforward than an inclusive model.<div><br></div><div>If I understand you correctly, with this model:</div><div><br></div><div></div><blockquote type="cite"></blockquote></div><div style="word-wrap:break-word"><blockquote type="cite"><div>enum NetworkException {<br> case NoInternetError, SecurityError<br>}</div><div><br></div></blockquote></div><div style="word-wrap:break-word"><blockquote type="cite"><div><span style="font-family:LucidaGrande">enum ChangePictureError {</span><br style="font-family:LucidaGrande"><span style="font-family:LucidaGrande"> case NetworkException(NetworkException)</span><br style="font-family:LucidaGrande"><span style="font-family:LucidaGrande"> case ParseException(ParseException)</span><br style="font-family:LucidaGrande"><span style="font-family:LucidaGrande"> case PictureTooLarge</span><br style="font-family:LucidaGrande"><span style="font-family:LucidaGrande">}</span><br></div></blockquote></div><div style="word-wrap:break-word"><blockquote type="cite"></blockquote><div><br></div>you're saying that we should be able to write:<div><br></div><div><blockquote type="cite">let x: ChangePictureError = NetworkException.NoInternetError<br></blockquote><div><div><br></div><div>The implicit conversion from NetworkException to ChangePictureError reminds me of C++ implicit constructors, which are generally frowned upon, so I'm not sure that this is the best way forward.</div><div><br></div><div>On the other hand, going back to my original example:</div></div></div></div><div style="word-wrap:break-word"><div><div><div><br></div><div><blockquote type="cite">enum MyLibError: ErrorType {<br><span style="white-space:pre-wrap">        </span>case FileNotFound<br><span style="white-space:pre-wrap">        </span>case UnexpectedEOF<br><span style="white-space:pre-wrap">        </span>case PermissionDenied<br><span style="white-space:pre-wrap">        </span>// ... 300 cases later<br><span style="white-space:pre-wrap">        </span>case FluxCapacitorFailure<br><span style="white-space:pre-wrap">        </span>case SplineReticulationError<br>}<br><br>enum FileSystemError: MyLibError {<br><span style="white-space:pre-wrap">        </span>case FileNotFound = .FileNotFound<br><span style="white-space:pre-wrap">        </span>case UnexpectedEOF = .UnexpectedEOF<br><span style="white-space:pre-wrap">        </span>case PermissionDenied = .PermissionDenied<br>}</blockquote></div><div><br></div></div></div></div><div style="word-wrap:break-word"><div><div><div>I can easily rationalize that FileSystemError is implicitly convertible to MyLibError because of the "inheritance" relationship.</div><div><br></div><div></div></div></div></div><div style="word-wrap:break-word"><div><div><div>Félix</div></div></div></div><div style="word-wrap:break-word"><div><div><div><br><div><br><div><blockquote type="cite"><div>Le 19 déc. 2015 à 14:28:44, Matthew Johnson <<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>> a écrit :</div><br><div><blockquote type="cite" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br>On Dec 18, 2015, at 11:34 AM, Dennis Lysenko via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><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' 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'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 "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><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's say the API operation is "change profile picture":</div><div><br></div><div>enum ChangePictureError {</div><div> include NetworkException</div><div> include ParseException</div><div> case PictureTooLarge</div><div>}</div></div></div></blockquote><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">By including all of the cases you make it possible for ChangePictureError to be a supertype of NetworkException and ParseException. This is a pretty interesting idea. It might be worth exploring. </div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">One thing that would need to be considered is that ideally if the actual values was a NetworkException case you would want to be able to call any methods exposed by Network Exception. A good way to accomplish that might be to add implicit conversion as well as syntactic sugar for nested enums. So if we have this:</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">enum ChangePictureError {<br> case NetworkException(NetworkException)<br> case ParseException(ParseException)<br> case PictureTooLarge<br>}</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">I can do this:</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">var error: ChangePictureError // set somewhere, can be set with a NetworkException or a PictureTooLarge</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">switch error {</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .NoNetworkError: // equivaluent to case .NetworkException(.NoNetworkError)</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .NoInternetError: // equivaluent to case .NetworkException(. NoInternetError)</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .FailedResponse(let statusCode): // equivaluent to case .ParseException(.FailedResponse(let statusCode))</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .EmptyResponse: // equivaluent to case .ParseException(.EmptyResponse)</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .MissingField(let fieldName): // equivaluent to case .ParseException(. MissingField(let fieldName))</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">case .PictureTooLarge:</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">}</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">The syntactic sugar would only work for case names where there is no overlap. Case names that overlap would need to be explicitly disambiguated. The syntactic sugar and implicit conversions could allow for either single-level nesting or arbitrary nesting depth. An example of arbitrary depth might be ParseException also containing a ValidationError case:</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">enum ValidationError {</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> case OutOfRange</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> case InvalidType</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">}</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">enum ParseException {</div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> case ValidationError(ValidationError)<br> case FailedResponse(statusCode: Int)<br> case EmptyResponse<br> case MissingField(fieldName: String)<br><div dir="ltr"><div>}</div></div></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Mostly just thinking out loud here and exploring the idea. What do others think of this?</div><br style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><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 style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div dir="ltr">On Fri, Dec 18, 2015 at 12:21 PM Dennis Lysenko <<a href="mailto:dennis.s.lysenko@gmail.com" target="_blank">dennis.s.lysenko@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style: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 '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><div class="gmail_quote"><div dir="ltr">On Fri, Dec 18, 2015 at 11:29 AM Félix Cloutier <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style: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>> // Discriminated unions<br>> enum Foo {<br>> case Bar(Int)<br>> case Baz(String)<br>> }<br>><br>> // Lists of unique tokens (mixable with discriminated unions)<br>> enum Foo {<br>> case Frob<br>> case Nicate<br>> }<br>><br>> // Lists of raw values<br>> enum Foo: String {<br>> case Bar = "Bar"<br>> case Baz = "Baz"<br>> }<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't need it all.<br><br>> enum MyLibError: ErrorType {<br>> case FileNotFound<br>> case UnexpectedEOF<br>> case PermissionDenied<br>> // ... 300 cases later<br>> case FluxCapacitorFailure<br>> case SplineReticulationError<br>> }<br>><br>> enum FileSystemError: MyLibError {<br>> case FileNotFound = .FileNotFound<br>> case UnexpectedEOF = .UnexpectedEOF<br>> case PermissionDenied = .PermissionDenied<br>> }<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'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><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><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=eLFMrKDT8iBxZ-2Fbnk-2BZqvSchNN-2FvYXdceA0T7VxwkAe2BJg05It4YmJpYYhgeIun5oPlQyNvL7cz7Ifkc1ps77nyaEQghVfFbD5rF9yE6zh-2FbshhFf7qCpnVtCAyPTatimrftT-2F-2Fi-2BfGFlAZY4xseLkrncBxqtQxBL5LYVkzivj4hUQ8LoGK3f-2F6NT0H-2F9kNZh0yDG-2Fq2VomQ5Pu2L-2FS9Kl3p2-2FA4iFQ7rinRdq-2FG34-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div></blockquote></div><br></div></div></div></div></div></blockquote></div>