<div dir="ltr"><div><font color="#bb2ca2" face="Menlo"><span style="font-size:11px">func</span></font><font face="Menlo"><span style="font-size:11px"> call(completion: (() </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">throws</span><span style="font-family:Menlo;font-size:11px"> -> Value) -> Void) { ... }</span><br></div><div><span style="font-family:Menlo;font-size:11px"><br></span></div><div>With completion handlers I quite like this approach, I don't think anyone has brought it up yet. The nice thing is that it's concise, consistent and you can do it with existing functionality. You can also defer expensive calculations if you only care that it did complete, not whether it succeeded.</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">// example:</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">typealias</span> Value = <span style="color:rgb(112,61,170)">Int</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">enum</span> SomeError: <span style="color:rgb(112,61,170)">ErrorType</span> { <span style="color:rgb(187,44,162)">case</span> Failed }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">var</span> queue = <span style="color:rgb(112,61,170)">Array</span><()-><span style="color:rgb(112,61,170)">Void</span>>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span> doItLater(shouldFail: <span style="color:rgb(112,61,170)">Bool</span>, completion: (() <span style="color:rgb(187,44,162)">throws</span> -> <span style="color:rgb(79,129,135)">Value</span>) -> <span style="color:rgb(112,61,170)">Void</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">queue</span>.<span style="color:rgb(61,29,129)">append</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">if</span> shouldFail {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> completion { <span style="color:rgb(187,44,162)">throw</span> <span style="color:rgb(79,129,135)">SomeError</span>.<span style="color:rgb(49,89,93)">Failed</span> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">else</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> completion { <span style="color:rgb(39,42,216)">123</span> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">// usage:</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)">doItLater<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(187,44,162)">false</span><span style="color:rgb(0,0,0)">) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">guard</span> <span style="color:rgb(187,44,162)">let</span> value = <span style="color:rgb(187,44,162)">try</span>? $0() <span style="color:rgb(187,44,162)">else</span> { <span style="color:rgb(187,44,162)">return</span> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(61,29,129)">print</span><span style="color:rgb(0,0,0)">(</span>"it worked! <span style="color:rgb(0,0,0)">\</span>(<span style="color:rgb(0,0,0)">value</span>)"<span style="color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)">doItLater<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(187,44,162)">true</span><span style="color:rgb(0,0,0)">) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">do</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(<span style="color:rgb(209,47,27)">"it worked! </span>\<span style="color:rgb(209,47,27)">(</span><span style="color:rgb(187,44,162)">try</span> $0()<span style="color:rgb(209,47,27)">)"</span>)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">catch</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(<span style="color:rgb(209,47,27)">"it failed </span>\<span style="color:rgb(209,47,27)">(</span>error<span style="color:rgb(209,47,27)">)"</span>)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(79,129,135)">queue</span>.<span style="color:rgb(61,29,129)">forEach</span> { $0() }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p></div><div>P.S.</div><div>Sorry, I've resurrected this from the archive as I joined the mailing list too late.</div><div><br></div><div>In-Reply-To: <<a href="mailto:C96096A9-E9F7-4566-A412-0E9E3B724DC9@fifthace.com">C96096A9-E9F7-4566-A412-0E9E3B724DC9@fifthace.com</a>><br></div><div>Message-ID: <CAN9tzpDy6-_gy=<a href="mailto:GV_6oR7_Nr3P6Mrk7pdEjFwS9xtDjmoJN_Uw@mail.gmail.com">GV_6oR7_Nr3P6Mrk7pdEjFwS9xtDjmoJN_Uw@mail.gmail.com</a>><br></div><div><br></div><div>I can't work out how to get gmail to set the headers. I apologise I'm a mailman noob.</div><br>> There's a concurrency proposal that discusses various options here:<br>> <a href="https://github.com/apple/swift/blob/master/docs/proposals/Concurrency.rst">https://github.com/apple/swift/blob/master/docs/proposals/Concurrency.rst</a><br>> <br>> I like the idea of using try-catch for asynchronous error handling, but<br>> don't think that defers needs to annotate throws. With a language-level<br>> async/await model we'd get deferred catching for free:<br>> <br>> class Request {<br>> async func fetch() throws -> Response {}<br>> }<br>> <br>> try {<br>> let response = try await request.fetch()<br>> } catch {<br>> // handle error<br>> }<br>> <br>> The "try await ?" is a bit wordy, though.<br>> <br>> On Fri, Dec 4, 2015 at 2:19 PM, Sean Heber <sean at <a href="http://fifthace.com">fifthace.com</a>> wrote:<br>> <br>>> I think that the code duplication issue is more of an API design side<br>>> effect. If the API was designed with a 3rd block for when it is ?done?<br>>> regardless of error, then you could put that shared code there - but I<br>>> would imagine that?d be out of the scope of Swift itself. What I often do<br>>> in these situations is just make a local function that does the shared<br>>> stuff and then call it from both blocks:<br>>><br>>> func finished() {<br>>> activityIndicator.stopAnimating()<br>>> button.enabled = true<br>>> tableView.hidden = false<br>>> }<br>>><br>>> request.fetch(withCompletionHandler: { result in<br>>> finished()<br>>> use(result)<br>>> }, errorHandler: { error in<br>>> finished()<br>>> handleFetchError(error)<br>>> })<br>>><br>>> l8r<br>>> Sean<br>>><br>>><br>>>> On Dec 4, 2015, at 1:13 PM, Dan Stenmark <daniel.j.stenmark at <a href="http://gmail.com">gmail.com</a>><br>>>wrote:<br>>>><br>>>> Hey Alex!<br>>>><br>>>> I completely agree that the double-block approach is advantageous for<br>>>its reduced unwrapping, but the only issue I have is that, when applied in<br>>>production, it typically leads to duplicate code.<br>>>><br>>>> request.fetch(withCompletionHandler: { result in<br>>>> activityIndicator.stopAnimating()<br>>>> button.enabled = true<br>>>> tableView.hidden = false<br>>>><br>>>> use(result)<br>>>><br>>>> print( ?Foo!? )<br>>>><br>>>> }, errorHandler: { error in<br>>>> activityIndicator.stopAnimating()<br>>>> button.enabled = true<br>>>> tableView.hidden = false<br>>>><br>>>> // Don?t need to force unwrap `error`.<br>>>> handleFetchError(error)<br>>>><br>>>> print( ?Foo!? )<br>>>><br>>>> })<br>>>><br>>>> The same issue is present in the aforementioned deferred throw concept.<br>>>There?re certainly ways to mitigate the problem, but no practical way of<br>>>eliminating it altogether. I?m personally leaning towards an evolution of<br>>>the Enum-based approach for this very reason, but if a solution can be<br>>>figured out to remove such code duplication, I?d happily adopt<br>>>double-blocks.<br>>>><br>>>> Dan<br>>>><br>>>>> On Dec 4, 2015, at 10:20 AM, Vinicius Vendramini <vinivendra at <a href="http://gmail.com">gmail.com</a>><br>>>wrote:<br>>>>><br>>>>> I really like the idea of using try/catches, it seems like a more<br>>>swifty approach. However, I'd prefer to do it in a way that doesn't add a<br>>>new syntax.<br>>>>><br>>>>> Also, I agree with Alex, the second options seems the best of those<br>>>alternatives (if it were done without trailing closures.)<br>>>>><br>>>>>> On Dec 4, 2015, at 12:21 PM, Sean Heber <sean at <a href="http://fifthace.com">fifthace.com</a>> wrote:<br>>>>>><br>>>>>> What if there was a possibility for a deferred throw?<br>>>>>><br>>>>>> For example, imagine the fetch function was something like this:<br>>>>>><br>>>>>> func fetch(withCompletionHandler: (Result) -> ()) defer throws {}<br>>>>>><br>>>>>> And to call it, you?d wrap it in a try with a new kind of catch:<br>>>>>><br>>>>>> do {<br>>>>>> try request.fetch() { result in ? }<br>>>>>> } defer catch (? pattern) {<br>>>>>> }<br>>>>>><br>>>>>> So what the language would do then is treat all of the ?defer catch?<br>>>blocks as closures and pass them along with calls to ?defer throws?<br>>>functions in the same context. When they throw, they would then run those<br>>>blocks as if you had passed them along manually in the double block style.<br>>>>>><br>>>>>> The context to throw back to would need to be capture-able by the<br>>>function body for fetch() so that if it encounters an error some time later<br>>>it would know where to throw it to. Perhaps the easiest way would be to<br>>>create a closure that throws like so:<br>>>>>><br>>>>>> func fetch(withCompletionHandler: (Result) -> ()) defer throws {<br>>>>>> var context = FetchContext()<br>>>>>> context.completionHandler = withCompletionHandler<br>>>>>> context.errorHandler = { reason in throw RequestError(reason) }<br>>>>>> self.pending.append(context)<br>>>>>> context.start()<br>>>>>> }<br>>>>>><br>>>>>> Under the hood the ?throw? captured in the closure would also be<br>>>carrying along the context needed to route that error back to the expected<br>>>defer catch block.<br>>>>>><br>>>>>> l8r<br>>>>>> Sean<br>>>>>><br>>>>>><br>>>>>>>> On Dec 4, 2015, at 10:57 AM, Alex Migicovsky <migi at <a href="http://apple.com">apple.com</a>> wrote:<br>>>>>>>><br>>>>>>>><br>>>>>>>>> On Dec 3, 2015, at 2:15 PM, Douglas Gregor <dgregor at <a href="http://apple.com">apple.com</a>><br>>>wrote:<br>>>>>>>>><br>>>>>>>>> On Dec 3, 2015, at 12:32 PM, Dan Stenmark <<br>>>daniel.j.stenmark at <a href="http://gmail.com">gmail.com</a>> wrote:<br>>>>>>>>><br>>>>>>>>> There?s a some of debate in the community regarding best practice<br>>>for asynchronous completion callbacks. These practices include:<br>>>>>>>>><br>>>>>>>>> - Single Block w/ Mutually Exclusive Result and Error Objects (the<br>>>current standard convention in Cocoa, though originally designed with<br>>>Objective-C in mind)<br>>>>>>>>> - Double Block (one for success, one for failure)<br>>>>>>>>> - Swift Enum w/ Associated Objects (as described here:<br>>><a href="http://www.developerdave.co.uk/2015/09/better-completion-handlers-in-swift/">http://www.developerdave.co.uk/2015/09/better-completion-handlers-in-swift/</a><br>>>)<br>>>>>>>>><br>>>>>>>>> Even prior to Swift, Apple?s code guidelines never explicitly<br>>>addressed this topic. Going forward into the brave new world of Swift, are<br>>>there going to be new preferred API design guidelines for this?<br>>>>>>>><br>>>>>>>> This is a great point, and there are a number of other issues<br>>>related to callbacks/closure arguments that would benefit from guidelines.<br>>>For example, I've seen the ?Double Block? case where the second block ends<br>>>up being a trailing closure, which makes for non-intuitive uses.<br>>>>>>><br>>>>>>> Hi Dan!<br>>>>>>><br>>>>>>> I think guidelines in this area would be great.<br>>>>>>><br>>>>>>> Here are the tradeoffs I think we have for each approach:<br>>>>>>><br>>>>>>> 1) The single block approach means you?d code against an optional<br>>>result and an optional error, making it easy to write invalid code (see<br>>>example in #2). With the single block you can use trailing closure syntax<br>>>coherently. I think most ObjC APIs use this approach since it works well in<br>>>ObjC.<br>>>>>>><br>>>>>>> 2) As Doug mentioned, the double block can be inconvenient / awkward<br>>>but it does produce more correct code.<br>>>>>>><br>>>>>>> Doug: maybe we can limit using trailing closures from being used if<br>>>the 2nd to last parameter is also a closure? That would eliminate some<br>>>confusion at the call site.<br>>>>>>><br>>>>>>> Some ObjC APIs use this approach. One positive aspect of the<br>>>double-block approach is that it always produces code that?s less indented<br>>>than the single block approach. e.g.<br>>>>>>><br>>>>>>> /*<br>>>>>>> Single block.<br>>>>>>> Trailing closure syntax works well.<br>>>>>>> */<br>>>>>>> request.fetch { result, error in<br>>>>>>> // More indented code since we need to use guard or if let.<br>>>>>>> guard let result = result else {<br>>>>>>> // Need to force unwrap `error`.<br>>>>>>> handleFetchError(error!)<br>>>>>>> return<br>>>>>>> }<br>>>>>>><br>>>>>>> use(result)<br>>>>>>> }<br>>>>>>><br>>>>>>> /*<br>>>>>>> Double block.<br>>>>>>> Trailing closure syntax is awkward here.<br>>>>>>> */<br>>>>>>> request.fetch(withCompletionHandler: { result in<br>>>>>>> use(result)<br>>>>>>> }, errorHandler: { error in<br>>>>>>> // Don?t need to force unwrap `error`.<br>>>>>>> handleFetchError(error)<br>>>>>>> })<br>>>>>>><br>>>>>>> 3) Enums with associated values are conceptually nice, but unless we<br>>>have a Result<> or an Either<> in the Standard Library I think most people<br>>>will write one-off enums for each set of methods that return a specific<br>>>kind of result + error. That adds an unnecessary conceptual burden since<br>>>you need to know the type of of the value that?s passed to each callback.<br>>>Also, we don?t have any primarily ObjC APIs that use this approach yet. It<br>>>would also suffer from the same indentation problem as #1 but without the<br>>>?invalid code? problem. If we go this route I think we?d want to map the<br>>>async error ObjC APIs to use this approach similar to what we do with<br>>>non-async error handling.<br>>>>>>><br>>>>>>> Looking at the tradeoffs I think I prefer #2 if we could limit the<br>>>ability to use a trailing closure for the last parameter. I?d want to look<br>>>at more code with the change though. We should also consider whether we<br>>>should map the single block APIs in ObjC into double block APIs. What do<br>>>you think?<br>>>>>>><br>>>>>>> Also, with any of these approaches there?s also the question of<br>>>whether we pass ErrorType, NSError, or the specific error type.<br>>>>>>><br>>>>>>> - Alex<br>>>>>>><br>>>>>>><br>>>>>>> _______________________________________________<br>>>>>>> swift-evolution mailing list<br>>>>>>> swift-evolution at <a href="http://swift.org">swift.org</a><br>>>>>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>>>>>><br>>>>>> _______________________________________________<br>>>>>> swift-evolution mailing list<br>>>>>> swift-evolution at <a href="http://swift.org">swift.org</a><br>>>>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>>>>> _______________________________________________<br>>>>> swift-evolution mailing list<br>>>>> swift-evolution at <a href="http://swift.org">swift.org</a><br>>>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>>>><br>>><br>>> _______________________________________________<br>>> swift-evolution mailing list<br>>> swift-evolution at <a href="http://swift.org">swift.org</a><br>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>>></div>