<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=""><div class="">Is there a way I can make that clearer than putting it in a section labeled “FUTURE DIRECTIONS”?</div><div class=""><br class=""></div><div class="">Charles</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 14, 2016, at 5:52 PM, Dan Appel &lt;<a href="mailto:dan.appel00@gmail.com" class="">dan.appel00@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Yes, it should be made clear that the 'async'/'await' suggestions are <i class="">future</i>&nbsp;<i class="">directions</i>, which are more to show how flexible the design is rather than actually be a part of the implementation.</div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Jul 14, 2016 at 3:30 PM Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Right, but since this would affect the Obj-C importer and thus would be a source-breaking change, it would probably not be possible anymore after Swift 3.<br class="">
<br class="">
Charles<br class="">
<br class="">
&gt; On Jul 14, 2016, at 4:57 PM, Dan Stenmark &lt;<a href="mailto:daniel.j.stenmark@gmail.com" target="_blank" class="">daniel.j.stenmark@gmail.com</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; I’d say it’s a little premature to be talking about this; the team has made it very clear that the discussion on Native Concurrency in Swift won’t begin for another couple months.<br class="">
&gt;<br class="">
&gt; Dan<br class="">
&gt;<br class="">
&gt;&gt; On Jul 14, 2016, at 10:54 AM, Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;<br class="">
&gt;&gt; I know it’s late, but I was wondering what the community thought of this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; MOTIVATION:<br class="">
&gt;&gt;<br class="">
&gt;&gt; With the acceptance of SE-0112, the error handling picture looks much stronger for Swift 3, but there is still one area of awkwardness remaining, in the area of returns from asynchronous methods. Specifically, many asynchronous APIs in the Cocoa framework are declared like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; - (void)doSomethingWithFoo: (Foo *)foo completionHandler: (void (^)(Bar * _Nullable, NSError * _Nullable))completionHandler;<br class="">
&gt;&gt;<br class="">
&gt;&gt; This will get imported into Swift as something like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Bar?, Error?) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; The intention of this API is that either the operation will succeed, and something will be passed in the Bar parameter, and the error will be nil, or else the operation will fail, and then the error parameter will be populated while the Bar parameter is nil. However, this intention is not expressed in the API, since the syntax leaves the possibility that both parameters could be nil, or that they could both be non-nil. This forces the developer to do needless and repetitive checks against a case which in practice shouldn’t occur, as below:<br class="">
&gt;&gt;<br class="">
&gt;&gt; doSomething(foo: foo) { bar, error in<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; if let bar = bar {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle success case<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; } else if let error = error {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.handleError(error)<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; } else {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.handleError(NSCocoaError.FileReadUnknownError)<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; This results in the dreaded “untested code.”<br class="">
&gt;&gt;<br class="">
&gt;&gt; Note that while it is possible that the developer could simply force-unwrap error in the failure case, this leaves the programs open to crashes in the case where a misbehaved API forgets to populate the error on failure, whereas some kind of default error would be more appropriate. The do/try/catch mechanism works around this by returning a generic _NilError in cases where this occurs.<br class="">
&gt;&gt;<br class="">
&gt;&gt; PROPOSED SOLUTION:<br class="">
&gt;&gt;<br class="">
&gt;&gt; Since the pattern for an async API that returns an error in the Cocoa APIs is very similar to the pattern for a synchronous one, we can handle it in a very similar way. To do this, we introduce a new Result enum type. We then bridge asynchronous Cocoa APIs to return this Result type instead of optional values. This more clearly expresses to the user the intent of the API.<br class="">
&gt;&gt;<br class="">
&gt;&gt; In addition to clarifying many Cocoa interfaces, this will provide a standard format for asynchronous APIs that return errors, opening the way for these APIs to be seamlessly integrated into future asynchronous features added to Swift 4 and beyond, in a way that could seamlessly interact with the do/try/catch feature as well.<br class="">
&gt;&gt;<br class="">
&gt;&gt; DETAILED DESIGN:<br class="">
&gt;&gt;<br class="">
&gt;&gt; 1. We introduce a Result type, which looks like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; enum Result&lt;T&gt; {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case success(T)<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case error(Error)<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; 2. Methods that return one parameter asynchronously with an error are bridged like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Result&lt;Bar&gt;) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; and are used like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; doSomething(foo: foo) { result in<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; switch result {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case let .success(bar):<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle success<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case let .error(error):<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.handleError(error)<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; 3. Methods that return multiple parameters asynchronously with an error are bridged using a tuple:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Result&lt;(Bar, Baz)&gt;) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; and are used like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; doSomething(foo: foo) { result in<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; switch result {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case let .success(bar, baz):<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle success<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; case let .error(error):<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.handleError(error)<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; 4. Methods that return only an error and nothing else are bridged as they are currently, with the exception of bridging NSError to Error as in SE-0112:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Error?) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; and are used as they currently are:<br class="">
&gt;&gt;<br class="">
&gt;&gt; doSomething(foo: foo) { error in<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; if let error = error {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle error<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; } else {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle success<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; 5. For the case in part 2, the bridge works much like the do/try/catch mechanism. If the first parameter is non-nil, it is returned inside the .success case. If it is nil, then the error is returned inside the .error case if it is non-nil, and otherwise _NilError is returned in the .error case.<br class="">
&gt;&gt;<br class="">
&gt;&gt; 6. For the case in part 3, in which there are multiple return values, the same pattern is followed, with the exception that we introduce a new Objective-C annotation. I am provisionally naming this annotation NS_REQUIRED_RETURN_VALUE, but the developer team can of course rename this annotation to whatever they find appropriate. All parameters annotated with NS_REQUIRED RETURN_VALUE will be required to be non-nil in order to avoid triggering the error case. Parameters not annotated with NS_REQUIRED RETURN_VALUE will be inserted into the tuple as optionals. If there are no parameters annotated with NS_REQUIRED RETURN_VALUE, the first parameter will be implicitly annotated as such. This allows asynchronous APIs to continue to return optional secondary values if needed.<br class="">
&gt;&gt;<br class="">
&gt;&gt; Thus, the following API:<br class="">
&gt;&gt;<br class="">
&gt;&gt; - (void)doSomethingWithFoo: (Foo *)foo completionHandler: (void (^)(Bar * _Nullable NS_REQUIRED_RETURN_VALUE, Baz * _Nullable NS_REQUIRED_RETURN_VALUE, NSError * _Nullable))completionHandler;<br class="">
&gt;&gt;<br class="">
&gt;&gt; is bridged as:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Result&lt;(Bar, Baz)&gt;) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; returning .success only if both the Bar and Baz parameters are non-nil, whereas this API:<br class="">
&gt;&gt;<br class="">
&gt;&gt; - (void)doSomethingWithFoo: (Foo *)foo completionHandler: (void (^)(Bar * _Nullable NS_REQUIRED_RETURN_VALUE, Baz * _Nullable, NSError * _Nullable))completionHandler;<br class="">
&gt;&gt;<br class="">
&gt;&gt; is bridged as:<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(foo: Foo, completionHandler: (Result&lt;(Bar, Baz?)&gt;) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; returning .success whenever the Bar parameter is nil. An API containing no parameter annotated with NS_REQUIRED_RETURN_VALUE will be bridged the same as above.<br class="">
&gt;&gt;<br class="">
&gt;&gt; FUTURE DIRECTIONS:<br class="">
&gt;&gt;<br class="">
&gt;&gt; In the future, an asynchronous API returning a Result could be bridged to an async function, should those be added in the future, using the semantics of the do/try/catch mechanism. The bridging would be additive, similarly to how Objective-C properties declared via manually written accessor methods can nonetheless be accessed via the dot syntax. Thus,<br class="">
&gt;&gt;<br class="">
&gt;&gt; func doSomething(_ completionHandler: (Result&lt;Foo&gt;) -&gt; ())<br class="">
&gt;&gt;<br class="">
&gt;&gt; could be used as if it were declared like this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; async func doSomething() throws -&gt; Foo<br class="">
&gt;&gt;<br class="">
&gt;&gt; and could be used like so:<br class="">
&gt;&gt;<br class="">
&gt;&gt; async func doSomethingBigger() {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; do {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let foo = try await doSomething()<br class="">
&gt;&gt;<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do something with foo<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; } catch {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle the error<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; making asynchronous APIs convenient to write indeed.<br class="">
&gt;&gt;<br class="">
&gt;&gt; ALTERNATIVES CONSIDERED:<br class="">
&gt;&gt;<br class="">
&gt;&gt; Leaving the somewhat ambiguous situation as is.<br class="">
&gt;&gt;<br class="">
&gt;&gt; Charles<br class="">
&gt;&gt;<br class="">
&gt;&gt; _______________________________________________<br class="">
&gt;&gt; swift-evolution mailing list<br class="">
&gt;&gt; <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
&gt;&gt; <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="">
&gt;<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><div dir="ltr" class="">-- <br class=""></div><div data-smartmail="gmail_signature" class=""><div dir="ltr" class=""><div class=""><div class="">Dan Appel<br class=""></div></div></div></div>
</div></blockquote></div><br class=""></body></html>