<div dir="ltr">Sorry, got a little excited in answering and just re-read the proposal. Ignore the part of "extra cruft", since I'll never have to see it unless I want to override it. I thought that the static dispatch bit later on in the proposal implies that that wouldn't work so well, but I might have misread. </div><br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 20, 2015 at 12:00 AM Dennis Lysenko <<a href="mailto:dennis.s.lysenko@gmail.com">dennis.s.lysenko@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Charles,<div><br></div><div>While I agree it's unfortunate that there isn't much interop between ErrorType and NSError, the decision from the core team to make ErrorType an empty protocol seems to have been a premeditated one. I would love to have someone on the core team respond here in my stead, but I'll try and present my side of it. </div><div><br></div><div>I have a lot of errors that come up internally in my app's workings. I subscribe to Swift's error handling rationale regarding recoverable errors. For example, my internal networking library has a NoInternetError which never needs to be presented using built-in cocoa error presentation, but instead has a different presentation scheme for each different context (e.g. if it happened while loading a tableview, it shows up as the placeholder text. if it happened in response to a user action, it displays an alert.) I, therefore, strongly disagree with adding any extra members to the ErrorType protocol: it's cruft that will never be of use to me.</div><div><br></div><div>Now, this is the part that I'm not sure about and I might be corrected by someone from the core team: It seems that part of the rationale for introducing the general ErrorType is to move <i>away</i> from NSError. NSError is antiquated and carries information that is often not even used (take a survey of 100 swift programmers, and maybe 40 of them will care to tell you what an error domain is). </div><div><br></div><div>A lot of this probably hinges on typed `throws` annotations as well; if we get those, then you never have to catch general ErrorType unless you wrote a function to throw general ErrorType, so you could make all your error types conform to a custom protocol with all this NSError-conformant info in it without worrying about static dispatch.</div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Dec 19, 2015 at 10:50 PM Charles Srstka via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</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">This is a bit of a pre-proposal; I wanted to bounce some ideas off the community before writing up a formal proposal, to see how people felt about this.<div><br></div><div>The Problem:</div><div><br></div><div>Swift introduces the very nifty ErrorType protocol, which, if implemented as an enum, allows one to associate arguments with the specific error cases with which they are relevant, as in this example:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">enum</span><span style="color:#000000"> MyError: </span>ErrorType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> JustFouledUp</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> CouldntDealWithFile(url: NSURL)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> CouldntDealWithSomeValue(value: Int)</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;line-height:normal">This is great, because it ensures that the file-related error will always have a URL object, whereas it won’t be included in the cases where it is irrelevant.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Unfortunately, the Cocoa APIs needed to display an error object to the user all take NSErrors, and the conversion from other ErrorTypes to NSErrors is not very good; using “as NSError” to convert a MyError will result in something that has all of the associated values removed, and the message displayed to the user will be a cryptic and not particularly user-friendly “MyError error 1” rather than something more descriptive. One can define an “toNSError()” method on one’s own error type that will properly propagate the NSError’s userInfo dictionary such that it will be displayed in a more meaningful way:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">enum</span><span style="color:rgb(0,0,0)"> MyError: </span>ErrorType<span style="color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> JustFouledUp</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> CouldntDealWithFile(url: NSURL)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> CouldntDealWithSomeValue(value: Int)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">func</span> toNSError() -> <span style="color:#703daa">NSError</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> userInfo = [<span style="color:#703daa">String</span> : <span style="color:#703daa">AnyObject</span>]()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">let</span> code: <span style="color:#703daa">Int</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">switch</span> <span style="color:#bb2ca2">self</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> .JustFouledUp:</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> userInfo[</span>NSLocalizedFailureReasonErrorKey<span style="color:#000000">] = </span><span style="color:#d12f1b">"Something fouled up!"</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> code = <span style="color:#272ad8">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> <span style="color:#bb2ca2">let</span> .CouldntDealWithFile(url):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> userInfo[</span>NSLocalizedFailureReasonErrorKey<span style="color:#000000">] = </span><span style="color:#d12f1b">"Something went wrong with the file </span><span style="color:#000000">\</span><span style="color:#d12f1b">(</span><span style="color:#000000">url.</span>lastPathComponent<span style="color:#000000"> ?? </span><span style="color:#d12f1b">"(null)")."</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSURLErrorKey</span>] = url</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> code = <span style="color:#272ad8">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> <span style="color:#bb2ca2">let</span> .CouldntDealWithSomeValue(value):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#000000"> userInfo[</span><span style="color:#703daa">NSLocalizedFailureReasonErrorKey</span><span style="color:#000000">] = </span>"This value isn't legit for some reason: <span style="color:#000000">\</span>(<span style="color:#000000">value</span>)"</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> code = <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> <span style="color:#703daa">NSError</span>(domain: <span style="color:#d12f1b">"MyError"</span>, code: code, userInfo: userInfo)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">However, since this method will only be invoked if called explicitly, one has to make sure to include .toNSError() every time when throwing an error object, or else it will not display properly; one can never just throw a MyError object. This is error-prone (no pun intended), and also prevents things like making the error conform to Equatable and comparing it against an ErrorType we received from some other method.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">I propose an addition to the ErrorType protocol, provisionally entitled “toNSError()”, but which another name could be given if something else is determined to be more appropriate. This method would be called by the system whenever “as NSError” is called on something that implements ErrorType. While this method would be added to the protocol, a default implementation would be provided in an extension, so that implementers of ErrorType would never actually need to override it unless very specific customization was required. For this default implementation, several other properties corresponding to some of the more commonly-used NSError keys (defined with default implementations returning nil) would be defined and referenced, and anything that returned non-nil would be packaged into a userInfo dictionary, like so:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">protocol</span> <span style="color:#703daa">ErrorType</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> description: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> failureReason: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoverySuggestion: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoveryOptions: [<span style="color:#703daa">String</span>]? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoveryAttempter: <span style="color:#703daa">AnyObject</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> helpAnchor: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> underlyingError: <span style="color:#4f8187">ErrorType</span>? { <span style="color:#bb2ca2">get</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> URL: <span style="color:#703daa">NSURL</span>? { <span style="color:#bb2ca2">get</span> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">func</span> toNSError() -> <span style="color:#703daa">NSError</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span>ErrorType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> description: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> failureReason: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoverySuggestion: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoveryOptions: [<span style="color:#703daa">String</span>]? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> recoveryAttempter: <span style="color:#703daa">AnyObject</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> helpAnchor: <span style="color:#703daa">String</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> underlyingError: <span style="color:#4f8187">ErrorType</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> URL: <span style="color:#703daa">NSURL</span>? { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">func</span> toNSError() -> <span style="color:#703daa">NSError</span> {</div><div style="margin:0px;line-height:normal"><font face="Menlo"><span style="font-size:11px"> </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">let</span><font face="Menlo"><span style="font-size:11px"> domain = </span></font><font color="#008400" face="Menlo"><span style="font-size:11px">// do what “as NSError” currently does to generate the domain</span></font></div><div style="margin:0px;line-height:normal"><font face="Menlo"><span style="font-size:11px"> </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">let</span><font face="Menlo"><span style="font-size:11px"> code = </span></font><font color="#008400" face="Menlo"><span style="font-size:11px">// do what “as NSError” currently does to generate the code</span></font></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> userInfo = [<span style="color:#703daa">String</span> : <span style="color:#703daa">AnyObject</span>]()</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> description = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">description</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSLocalizedDescriptionKey</span>] = description</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> failureReason = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">failureReason</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSLocalizedFailureReasonErrorKey</span>] = failureReason</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> suggestion = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">recoverySuggestion</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> userInfo[</span>NSLocalizedRecoverySuggestionErrorKey<span style="color:#000000">] = suggestion</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> options = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">recoveryOptions</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> userInfo[</span>NSLocalizedRecoveryOptionsErrorKey<span style="color:#000000">] = options</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> attempter = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">recoveryAttempter</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSRecoveryAttempterErrorKey</span>] = attempter</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> anchor = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">helpAnchor</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSHelpAnchorErrorKey</span>] = anchor</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> underlying = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">underlyingError</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSUnderlyingErrorKey</span>] = underlying.<span style="color:#31595d">toNSError</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> url = <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">URL</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSURLErrorKey</span>] = url</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> url.<span style="color:#703daa">fileURL</span>, <span style="color:#bb2ca2">let</span> path = url.<span style="color:#703daa">path</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> userInfo[<span style="color:#703daa">NSFilePathErrorKey</span>] = path</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> <span style="color:#703daa">NSError</span>(domain: domain, code: code, userInfo: userInfo)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Thanks to all the default implementations, the error type would only have to implement the properties corresponding to the userInfo keys that the implementer deems relevant, as in:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">enum</span><span style="color:#000000"> MyError: </span>ErrorType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> JustFouledUp</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> CouldntDealWithFile(url: NSURL)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> CouldntDealWithSomeValue(value: Int)</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> failureReason: <span style="color:#703daa">String</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">switch</span> <span style="color:#bb2ca2">self</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> .JustFouledUp:</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#000000"> </span><span style="color:#bb2ca2">return</span><span style="color:#000000"> </span>"Something fouled up!"</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> <span style="color:#bb2ca2">let</span> .CouldntDealWithFile(url):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#000000"> </span><span style="color:#bb2ca2">return</span><span style="color:#000000"> </span>"Something went wrong with the file <span style="color:#000000">\</span>(<span style="color:#000000">url.</span><span style="color:#703daa">lastPathComponent</span><span style="color:#000000"> ?? </span>"(null)")."</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> <span style="color:#bb2ca2">let</span> .CouldntDealWithSomeValue(value):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#000000"> </span><span style="color:#bb2ca2">return</span><span style="color:#000000"> </span>"This value isn't legit for some reason: <span style="color:#000000">\</span>(<span style="color:#000000">value</span>)"</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">var</span> URL: <span style="color:#703daa">NSURL</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">switch</span> <span style="color:#bb2ca2">self</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">case</span> <span style="color:#bb2ca2">let</span> .CouldntDealWithFile(url):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> url</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">default</span>:</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px">This could then be created and passed to an API taking an NSError like so:</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="color:#bb2ca2">let</span> err = <span style="color:#4f8187">MyError</span>.<span style="color:#31595d">CouldntDealWithFile</span>(url: <span style="color:#703daa">NSURL</span>(fileURLWithPath: <span style="color:#d12f1b">"/path/to/file"</span>))</div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;min-height:13px"><br></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:#703daa">NSApp</span><span style="color:#000000">.</span>presentError<span style="color:#000000">(</span><span style="color:#4f8187">err</span><span style="color:#000000"> </span><span style="color:#bb2ca2">as</span><span style="color:#000000"> </span><span style="color:#703daa">NSError</span><span style="color:#000000">)</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:#000000"><br></span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">and everything would be properly presented to the user.</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;line-height:normal">Similar functionality could be added to the protocol for conversions in the other direction, although this would be more difficult and would require more work on the implementer’s part.</div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;line-height:normal">The biggest problem I see to the idea is the use of references to Foundation types—NSError and NSURL—in the ErrorType definition, which may be undesired in a pure-Swift environment. In particular, usage of the NSURL type for the ‘URL’ property, which could have useful applications outside of simple Obj-C interop, could be irksome. Normally I would just propose adding things in an extension, but of course in this case, declaring methods in protocol extensions causes them to be statically dispatched, which could result in the wrong methods being called if the caller thought it was looking at a generic ErrorType rather than the specific concrete type. Perhaps this could spark a new discussion on whether there ought to be a way to declare dynamically-dispatched methods in protocol extensions. It’s also possible that Swift could use a built-in URL type, equivalent to Foundation’s NSURL, eliminating the need for any NS types other than NSError here. It’s also possible that since there appears to be an open-source implementation of Foundation in the github repository, that this isn’t even an issue and is something we can just leave in. At any rate, I thought this might be an interesting starting point for discussion.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Of course, an alternative solution could be to define “domain”, “code”, and “userInfo” properties (I know the first two are already in there, but this would make them public) in the protocol and just use those. These could also get default implementations that would work similarly to what is above, which, if Swift gained a native URL type, could completely eliminate Foundation types from the public interface.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">What do you think?</div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">Charles</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></span></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=8CZIdLciSFC-2BO5jF-2FiP8qN7dBFsgCUZ50wdTsolcRPfd0dUIy-2B-2BVRhtoJl5jFmBdIWlVk4We1bAu2yiFfU42dUPQ7fTWfSJFPVGicMEAo58Yk4Szeexy0335gvU1h-2BMOrAyZTLT0sNTR8AluwLD-2BlaKzTZypX6nN8Yi8xace4KICkJVzjOFuRi5UTFWiOfhLOL5VJz0m5Yx79WkB3KUJcr967aSZzaXTcSaV-2B5my990-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">
</div>
_______________________________________________<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>