<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 05:18:47 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>mikeash - 2017-07-14 17:08:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>I think you can do it even better with Swift errors, since you can write a single generic adapter function instead of writing a bunch of adapter methods. For example:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;func handle&amp;lt;T&amp;gt;(_ call: @autoclosure () throws -&amp;gt; T, errorHandler: (Error) -&amp;gt; Void, continuation: (T) -&amp;gt; Void) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;do {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;let value = try call()
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;continuation(value)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} catch {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;errorHandler(error)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;enum E: Error {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case e
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;func f() throws -&amp;gt; String {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return "Hello, world"
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;func g() throws -&amp;gt; String {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw E.e
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;handle(try f(), errorHandler: {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("error: \($0)")
&lt;br /&gt;}, continuation: {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("success: \($0)")
&lt;br /&gt;})
&lt;br /&gt;handle(try g(), errorHandler: {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("error: \($0)")
&lt;br /&gt;}, continuation: {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("success: \($0)")
&lt;br /&gt;})&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;However, I think Swift's error handling system is good enough to obsolete this whole idea.</description><guid isPermaLink="true">5eb9ac81b6b3f115005513472f8d4ac8</guid><pubDate>Fri, 14 Jul 2017 17:08:26 GMT</pubDate></item><item><title>Doug - 2017-07-13 21:39:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>I like this a lot but now that Swift converts NSErrors to &lt;code&gt;throws&lt;/code&gt; it's hard to adopt :(
&lt;br /&gt;
&lt;br /&gt;&lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html"&gt;https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html&lt;/a&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;If the last non-block parameter of an Objective-C method is of type NSError **, Swift replaces it with the throws keyword, to indicate that the method can throw an error. If the Objective-C method’s error parameter is also its first parameter, Swift attempts to simplify the method name further, by removing the “WithError” or “AndReturnError” suffix, if present, from the first part of the selector. If another method is declared with the resulting selector, the method name is not changed.
&lt;br /&gt;
&lt;br /&gt;If an error producing Objective-C method returns a BOOL value to indicate the success or failure of a method call, Swift changes the return type of the function to Void. Similarly, if an error producing Objective-C method returns a nil value to indicate the failure of a method call, Swift changes the return type of the function to a nonoptional type.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description><guid isPermaLink="true">f5e1dd7fe37311174337deeb50fdd117</guid><pubDate>Thu, 13 Jul 2017 21:39:59 GMT</pubDate></item><item><title>Steve - 2012-01-25 06:01:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Interesting ideas... One of the downsides of CPS in Haskell and other FP languages is the nesting that inevitably occurs, as displayed by your example in the contents.  Monadic notation does a lot to fix this (do ...) - wonder if the same pattern can be applied here in order to come up with an even cleaner notation?  Need to define &amp;gt;&amp;gt;, &amp;gt;&amp;gt;= equivalents, and maybe a #define for the do notation...</description><guid isPermaLink="true">4d567bc8181e258909c33b4312715684</guid><pubDate>Wed, 25 Jan 2012 06:01:16 GMT</pubDate></item><item><title>mikeash - 2010-05-19 19:46:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>That is neat! I didn't know JQuery had that. Would translate almost perfectly into ObjC with blocks.</description><guid isPermaLink="true">a42c5a76bd85cee39eaf1127c0a1cb5b</guid><pubDate>Wed, 19 May 2010 19:46:55 GMT</pubDate></item><item><title>slf - 2010-05-19 14:07:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Using blocks in this way could make some kind of interesting Obj-C flavor of javascript.  jQuery aggressively uses this technique:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;$.ajax({
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;type: "POST",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;url: "some.php",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;data: "name=John&amp;amp;location=Boston",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;success: function(msg){
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;alert( "yay!" );
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;},
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;error: function(xr, s, e) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;alert( "aww :(" );
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;});
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://api.jquery.com/jQuery.ajax/"&gt;http://api.jquery.com/jQuery.ajax/&lt;/a&gt;</description><guid isPermaLink="true">e772101991ca13102afa6d2a030fd9b4</guid><pubDate>Wed, 19 May 2010 14:07:00 GMT</pubDate></item><item><title>mikeash - 2010-05-18 15:47:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Python can't return multiple values in the same way that C can't pass function parameters by reference. It's true, but existing language features combine to give you functionality that's indistinguishable. Python &lt;i&gt;effectively&lt;/i&gt; has multiple value return, even if it's actually just tuple packing and unpacking, the same way that C &lt;i&gt;effectively&lt;/i&gt; has pass-by-reference, even though it's actually just passing a pointer by value.</description><guid isPermaLink="true">8fccfb759288234c50f22806ecca2f25</guid><pubDate>Tue, 18 May 2010 15:47:58 GMT</pubDate></item><item><title>Filip van der Meeren - 2010-05-18 15:35:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>I disagree about your statement that Python has the ability to return multiple values.
&lt;br /&gt;
&lt;br /&gt;Python is able to return a list or tuple. Automatic unpacking allows you to unpack the array in multiple values:
&lt;br /&gt;
&lt;br /&gt;aTuple = ("value1", "value2", "value3", "value4")
&lt;br /&gt;value1, *value2and3, value4 = aTuple
&lt;br /&gt;
&lt;br /&gt;# Is the same as
&lt;br /&gt;aTuple = "value1", "value2", "value3", "value4"
&lt;br /&gt;value1, *value2and3, value4 = aTuple
&lt;br /&gt;
&lt;br /&gt;# Is the same as
&lt;br /&gt;def aFunction():
&lt;br /&gt;&amp;nbsp;&amp;nbsp;return "value1", "value2", "value3", "value4"
&lt;br /&gt;
&lt;br /&gt;value1, *value2and3, value4 = aFunction()
&lt;br /&gt;</description><guid isPermaLink="true">39e4ac2494f265d36a4cd83da95c209d</guid><pubDate>Tue, 18 May 2010 15:35:19 GMT</pubDate></item><item><title>Kevin Ballard - 2010-03-27 23:55:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>One potential pitfall I can see with doing heavy CPS style is that, IIRC, GCC does not do tail-call optimization, which means that each new continuation introduces a new stack frame. Stacks are pretty large these days so you're not likely to run into a problem, but the possibility is still there. Of course, if you're using LLVM I believe that does tail-call optimization now.</description><guid isPermaLink="true">a599aeea97195136ba6eebbe3ea7193e</guid><pubDate>Sat, 27 Mar 2010 23:55:39 GMT</pubDate></item><item><title>Remy Demarest (Psy|) - 2010-02-10 17:08:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>I've written this little code to add SmallTalkish methods to ObjC using blocks. The last part of the example shows a way to use continuations:
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://gist.github.com/239363"&gt;http://gist.github.com/239363&lt;/a&gt;</description><guid isPermaLink="true">e5670101f648590f4f5f38dc336c8420</guid><pubDate>Wed, 10 Feb 2010 17:08:21 GMT</pubDate></item><item><title>mikeash - 2010-02-08 17:10:41</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Raising an error makes sense. Thanks for the explanation.
&lt;br /&gt;
&lt;br /&gt;Objective-C blocks are already doing return type inference. Declaring the return type of a block is possible but not mandatory. This inference could be used for implicit returns as well. Really, implicit return is just a bit of syntactic sugar in this case. It's just like putting the &lt;code&gt;return&lt;/code&gt; keyword right before the last statement in the block.</description><guid isPermaLink="true">3a9a30fba6b45e318dbd4c3da5918009</guid><pubDate>Mon, 08 Feb 2010 17:10:41 GMT</pubDate></item><item><title>Karsten - 2010-02-08 08:56:01</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>in your example, if you evaluate the block later, this will raise an error that the context cannot return.
&lt;br /&gt;
&lt;br /&gt;As for the implicit return of blocks. That would be great, but that just doesn't work in the c-based Objective-C, where you explicitly need to declare the return type.</description><guid isPermaLink="true">4c32831f488f717f2a3e6c90781c3219</guid><pubDate>Mon, 08 Feb 2010 08:56:01 GMT</pubDate></item><item><title>mikeash - 2010-02-08 03:41:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>An interesting question. The best I can come up with is that returning the string directly forces you to have an if statement, which is avoided with the pure CPS approach. E.g.:
&lt;br /&gt;
&lt;br /&gt;NSString *myString = [obj ...message];
&lt;br /&gt;if(myString)
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// it's good
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;Versus:
&lt;br /&gt;
&lt;br /&gt;[obj ...message: ^(NSString *myString) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// it's good
&lt;br /&gt;}];
&lt;br /&gt;
&lt;br /&gt;The rest is basically the same amount of code, so you save the if statement. You also lose the convenience of having code flow return directly to your method body....</description><guid isPermaLink="true">58d41e518d341537422a5133ea5f4e7b</guid><pubDate>Mon, 08 Feb 2010 03:41:59 GMT</pubDate></item><item><title>Scott Little - 2010-02-08 00:08:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>I really like this idea a lot, but there is one thing I don't really see.... The need for the second part, i.e. the continuation block. If you have this signature:
&lt;br /&gt;
&lt;br /&gt;stringWithContentsOfFile:encoding:errorHandler:continuation:
&lt;br /&gt;
&lt;br /&gt;I don't really see the advantage to having the continuation block at all! If I don't error give me back the string and let my program continue its normal flow, but let the block handle any errors.
&lt;br /&gt;
&lt;br /&gt;How does that second block help me out?
&lt;br /&gt;
&lt;br /&gt;Thanks for the great posts.</description><guid isPermaLink="true">49dc8eb272c18361f59985cefa9fd705</guid><pubDate>Mon, 08 Feb 2010 00:08:42 GMT</pubDate></item><item><title>mikeash - 2010-02-06 17:59:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>My question about the return operator was more like this (forgive my presumably-poor syntax, since I've never really written Smalltalk):
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;Person&amp;gt;&amp;gt;something
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;^[^nil]&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;In other words, you return the block from the enclosing method, then call it somewhere later, but it still contains a &lt;code&gt;^&lt;/code&gt; statement. I know this probably makes no sense to do intentionally, but I'm wondering what happens if you try it anyway.
&lt;br /&gt;
&lt;br /&gt;Given the discussion about Smalltalk, I'm finding myself wishing that Objective-C blocks had the same semantic where the block's value is simply the value of its last expression. Having to write return everywhere is annoying and sometimes confusing.</description><guid isPermaLink="true">f4ba8d28e287982dd0d167a96bb41bd0</guid><pubDate>Sat, 06 Feb 2010 17:59:31 GMT</pubDate></item><item><title>Karsten - 2010-02-06 17:10:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Coming from Smalltalk, i've also enjoyed using error-blocks as arguments a lot. In Smalltalk querying a dictionary for a key that's not in the dictionary raises an error. So the common way of getting values out of a dictionary is not #objectForKey: but more along the lines of #objectForKey:ifAbsent:. The second parameter is a block that is evaluated when the key is absent.
&lt;br /&gt;
&lt;br /&gt;I'm not sure if that already addresses your question about the return operator before, but you can write a method #street like this in a class Person:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;Person&amp;gt;&amp;gt;street
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;| address |
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;address := addresses at: self ifAbsent:[^nil].
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;^address street
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;If there is no address for the given person, it'll execute the error-block. In that block is a return, so the #street method returns nil. If an address was found, the #at:ifAbsent: would return this address and would not evaluate its error-block. 
&lt;br /&gt;Thus the method does only return after "address street" is evaluated.</description><guid isPermaLink="true">29d0e2e056e54e56bb6c92a01d22eb77</guid><pubDate>Sat, 06 Feb 2010 17:10:13 GMT</pubDate></item><item><title>Nikita Zhuk - 2010-02-06 12:04:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Your last example looks like the most readable, but you're correct that it probably isn't the most realistic example.
&lt;br /&gt;
&lt;br /&gt;I really like the idea of CPS, but I think (at least with current syntax) it decreases the readability of the code. In first example, the indentation level (which is actually conditional nesting level in this case) grows from 1 to 4, in second example from 1 to 2 and requires the developer to break the method into smaller ones, which may or may not be desirable (if the original method is already performing a single semantic thing and all method calls are on the same abstraction level, it may not make sense to break it down).</description><guid isPermaLink="true">938ff206ea40d381d1a8b07418908c79</guid><pubDate>Sat, 06 Feb 2010 12:04:32 GMT</pubDate></item><item><title>mikeash - 2010-02-06 07:36:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>An interesting question. This is a basic translation:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (BOOL)doSomething 
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;__block BOOL returnValue = NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj objectWhateverWithErrorHandler: ^(NSError *) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError: error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;continuation: ^(id obj1) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj1 objectWhateverWithErrorHandler: ^(NSError *) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError: error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;continuation: ^(id obj2) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj2 objectWhateverWithErrorHandler: ^(NSError *) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError: error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;continuation: ^(id obj3) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;returnValue = YES;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return returnValue;
&lt;br /&gt;}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;The nesting does get a bit ugly, but I don't find it to be too horrible. In reality, I would probably rewrite it as a series of smaller methods:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (BOOL)doSomething
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;__block BOOL returnValue = NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj objectWhateverWithErrorHandler: ^(NSError *) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError: error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;continuation: ^(id obj1) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;returnValue = [self doSomethingWithObj1: obj1];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return returnValue;
&lt;br /&gt;}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;If you want to get fancy, take advantage of the fact that your three error handlers are all identical, and define that handler up front so you just pass a variable in to each one:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (BOOL)doSomething
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;__block BOOL returnValue = NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void (^errorHandler)(NSError *) = ^(NSError *) { [self _handleError: error]; };
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj objectWhateverWithErrorHandler: errorHandler continuation: ^(id obj1) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj1 objectWhateverWithErrorHandler: errorHandler continuation: ^(id obj2) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[obj2 objectWhateverWithErrorHandler: errorHandler continuation: ^(id obj3) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;returnValue = YES;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return returnValue;
&lt;br /&gt;}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;I find this to look &lt;i&gt;really&lt;/i&gt; nice, but I don't know how likely it would be to find a situation where you could use it in real code.</description><guid isPermaLink="true">4bd97def3e1925574aed2ade4b75ab0f</guid><pubDate>Sat, 06 Feb 2010 07:36:58 GMT</pubDate></item><item><title>Nikita Zhuk - 2010-02-06 07:07:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>How the CPS style affects readability of a method which has several method calls which may return NSError values and which are handled within the method, i.e. the method doesn't pass NSError up to the caller? Imagine the following method written with standard error conventions:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;- (BOOL)doSomething
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSError *error = nil;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id obj1 = [obj objectWhateverWithError:&amp;amp;error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(obj1 == nil)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError:error]
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id obj2 = [obj1 objectWhateverWithError:&amp;amp;error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(obj2 == nil)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError:error]
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id obj3 = [obj2 objectWhateverWithError:&amp;amp;error];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(obj3 == nil)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self _handleError:error]
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return NO;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return YES;
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;How this method would look like in CPS style?</description><guid isPermaLink="true">7f8f394af2503ac80a5c6c7eefe7e8bf</guid><pubDate>Sat, 06 Feb 2010 07:07:19 GMT</pubDate></item><item><title>mikeash - 2010-02-06 06:32:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>No, you can put it wherever you want. Like (most of) the rest of C, all whitespace is equivalent. My preference is to leave it on the same line, but I couldn't tell you why that's different from everywhere else....</description><guid isPermaLink="true">6b04cb5e1badc4b2d6d301c82cdea62d</guid><pubDate>Sat, 06 Feb 2010 06:32:42 GMT</pubDate></item><item><title>dwineman - 2010-02-06 06:18:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Does the blocks syntax require the open-brace to be on the same line as the ^(...) ? If not, it might be slightly more readable without the mixed brace style.</description><guid isPermaLink="true">50def5cbe2fa4717188233d2d209984e</guid><pubDate>Sat, 06 Feb 2010 06:18:59 GMT</pubDate></item><item><title>mikeash - 2010-02-05 22:12:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Well, Apple conventions don't get everything right, and as long as you don't let exceptions leak into code you don't control, then it doesn't matter what Apple wants you to do, the question is simply whether they work well for you. Of course, Apple's guidelines and the fact that the community mostly follows them means that almost any third-party code you work with won't be exception-safe, which will make using them routinely considerably harder.</description><guid isPermaLink="true">9c798f1e2cb7e8aa5c08a3881ff75073</guid><pubDate>Fri, 05 Feb 2010 22:12:19 GMT</pubDate></item><item><title>August Joki - 2010-02-05 21:56:23</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>If I'm not mistaken Apple and/or convention has dictated that exceptions are meant for programming/-er errors, not for any old error. Another reason not to use exceptions to return useful runtime information.</description><guid isPermaLink="true">0223f0117622233cb329900aa33d7725</guid><pubDate>Fri, 05 Feb 2010 21:56:23 GMT</pubDate></item><item><title>mikeash - 2010-02-05 21:56:04</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>&lt;b&gt;Nicholas Riley:&lt;/b&gt; The &lt;code&gt;NSWorkspace&lt;/code&gt; methods aren't exact analogs, because they're using CPS for asynchronicity, and if you're writing asynchronous code then you &lt;i&gt;have&lt;/i&gt; to use a block, function pointer, object/selector pair, etc. callback, and pass the results into it. Using blocks for synchronous continuations is fairly different, although this does show that Apple isn't afraid of this sort of thing.
&lt;br /&gt;
&lt;br /&gt;Interesting note about Smalltalk. What happens if you use the &lt;code&gt;^&lt;/code&gt; return operator after the enclosing scope has already returned?
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;jrk:&lt;/b&gt; One man's elegance is another man's unreadable, I suppose. Care to elaborate on why you find this to be unreadable?</description><guid isPermaLink="true">3a65fcd1f7b16c6fdbee39b3163ea6d4</guid><pubDate>Fri, 05 Feb 2010 21:56:04 GMT</pubDate></item><item><title>jrk - 2010-02-05 21:17:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>well, it may be a nice way of doing it but overusing blocks makes the code unreadable.
&lt;br /&gt;
&lt;br /&gt;I'd rather type 10 lines than have unreadable code. :(</description><guid isPermaLink="true">ca2d84b5d717304decc616a979a24302</guid><pubDate>Fri, 05 Feb 2010 21:17:02 GMT</pubDate></item><item><title>Nicholas Riley - 2010-02-05 20:51:57</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Thanks, good topic. A couple of mental comparisons this dislodged:
&lt;br /&gt;
&lt;br /&gt;First, the two-callback-based system that Twisted Deferreds use ("callback" and "errback"):  &lt;a href="http://twistedmatrix.com/documents/9.0.0/api/twisted.internet.defer.Deferred.html"&gt;http://twistedmatrix.com/documents/9.0.0/api/twisted.internet.defer.Deferred.html&lt;/a&gt;) is an asynchronous implementation of what you describe, but it makes sense to see it used synchronously too.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;NSWorkspace&lt;/code&gt; does have a couple of methods that provide an &lt;code&gt;NSError&lt;/code&gt; to the callback (&lt;code&gt;recycleURLs:&lt;/code&gt; and &lt;code&gt;duplicateURLs:&lt;/code&gt;), as in your first example, so there is some possibility that this style will be used more in future.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (void)recycleURLs:(NSArray *)URLs completionHandler:(void (^)(NSDictionary *newURLs, NSError *error))handler AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (void)duplicateURLs:(NSArray *)URLs completionHandler:(void (^)(NSDictionary *newURLs, NSError *error))handler AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Finally, your comment about &lt;code&gt;return&lt;/code&gt; not returning from the containing function made me laugh—I was just reading a blog entry where someone was trying to address Smalltalk having the opposite problem. Of course, Smalltalk being Smalltalk, it was actually possible to work around this by adding a method: &lt;a href="http://www.cincomsmalltalk.com/userblogs/mls/blogView?entry=3441567670"&gt;http://www.cincomsmalltalk.com/userblogs/mls/blogView?entry=3441567670&lt;/a&gt;.</description><guid isPermaLink="true">dddcc2474bc5986c841e6d28e9201235</guid><pubDate>Fri, 05 Feb 2010 20:51:57 GMT</pubDate></item><item><title>mikeash - 2010-02-05 20:01:37</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Yeesh, I have no idea how I was able to make that typo so many times. Fixed, and thanks!</description><guid isPermaLink="true">b1dc8f714143e9cfd2ec40109902eb57</guid><pubDate>Fri, 05 Feb 2010 20:01:37 GMT</pubDate></item><item><title>Sean - 2010-02-05 19:54:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Thought you might want to know that you've got quite a few spots where you spell continuation as "contituation", which may not have been your intent.</description><guid isPermaLink="true">e8ea39661a58653cd18c3cb580095b69</guid><pubDate>Fri, 05 Feb 2010 19:54:21 GMT</pubDate></item><item><title>Grzegorz Adam Hankiewicz - 2010-02-05 19:49:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-05-error-returns-with-continuation-passing-style.html#comments</link><description>Man, you got me there until "This is nice and straightforward".</description><guid isPermaLink="true">4a85e5ff6b27d8b03fbeb289419fa7fb</guid><pubDate>Fri, 05 Feb 2010 19:49:45 GMT</pubDate></item></channel></rss>
