<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-02-26-futures.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Thu, 12 Nov 2026 22:37:57 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Steven - 2012-03-21 10:19:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>You can solve the block signature mismatch by making the return type explicit:
&lt;br /&gt;
&lt;br /&gt;MALazyFuture((id) ^ () { return title; });</description><guid isPermaLink="true">a9e51121f871b0306b134894169a7562</guid><pubDate>Wed, 21 Mar 2012 10:19:20 GMT</pubDate></item><item><title>mikeash - 2010-02-28 19:49:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>Are there any performance techniques which &lt;i&gt;don't&lt;/i&gt; encourage developers to restructure code for performance? If your goal is faster code, there are going to be compromises, that's just how it is. If you prefer legibility over performance, that's perfectly reasonable, but that does mean that the performance techniques available to you will be extremely limited!</description><guid isPermaLink="true">2269c781817a3135d661fc6ff6e27cd6</guid><pubDate>Sun, 28 Feb 2010 19:49:59 GMT</pubDate></item><item><title>Peter - 2010-02-28 18:43:41</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>There is one problem I see with this approach:
&lt;br /&gt;In order to maximize parallelism, the Future has to be created as early as possible and used as late as possible.  That encourages developers to restructure code for performance instead of legibility.  This could be done by the compiler, but obviously the compiler doesn't know about the internals.
&lt;br /&gt;It's a bit of a pity, because it's a very nice approach.</description><guid isPermaLink="true">c8dace4933b09fe9a60547355d0669dc</guid><pubDate>Sun, 28 Feb 2010 18:43:41 GMT</pubDate></item><item><title>Remy "Psy" Demarest - 2010-02-27 02:13:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>Okay, fair enough, though the -isProxy method is specifically here to tell whether you should trust -class as being the object you're talking or not. IMHO It's a little weird to only be able to know the class of an object through runtime functions, even for proxy object. But of course that's a choice to make, too.</description><guid isPermaLink="true">d2ac47e4d95e8ff176337e5ae3d2e858</guid><pubDate>Sat, 27 Feb 2010 02:13:58 GMT</pubDate></item><item><title>mikeash - 2010-02-26 22:43:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>&lt;code&gt;-[NSProxy class]&lt;/code&gt; returns the proxy class.
&lt;br /&gt;
&lt;br /&gt;Which behavior is correct is definitely debatable. Code which checks &lt;code&gt;class&lt;/code&gt; and then starts accessing instance variables directly if it matches will fail hard if you proxy that message. On the other hand, code which checks &lt;code&gt;class&lt;/code&gt; as a first check for equality will fail if you &lt;i&gt;don't&lt;/i&gt; proxy it.
&lt;br /&gt;
&lt;br /&gt;Ultimately I lean towards making the proxy look as much like the original object as possible, which means proxying &lt;code&gt;class&lt;/code&gt;.</description><guid isPermaLink="true">28c9369728fc745b97dcd35d8d34c5a1</guid><pubDate>Fri, 26 Feb 2010 22:43:15 GMT</pubDate></item><item><title>Remy "Psy" Demarest - 2010-02-26 21:37:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>What does NSProxy instances return when you send them a -class message ?
&lt;br /&gt;The documentation for -isProxy says this method is required because -isMemberOfClass: and -isKindOfClass: methods do type-check on their proxied object. I would suppose then that -class would return the class of the proxy itself.
&lt;br /&gt;
&lt;br /&gt;So you could do the same thing in your code, supposing people always do actual type-check using -isMemberOfClass: and -isKindOfClass: and not [obj class] == [MyClass class].</description><guid isPermaLink="true">5841cfd170125b6087c460f66da46d2b</guid><pubDate>Fri, 26 Feb 2010 21:37:58 GMT</pubDate></item><item><title>mikeash - 2010-02-26 21:10:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>&lt;b&gt;Remy:&lt;/b&gt; The main reason I didn't use those functions is simply because I didn't know about them. I still like the inline reference count because it's (presumably) faster, and won't have the spinlock contention problems of the Cocoa implementation, but it's really not important either way.
&lt;br /&gt;
&lt;br /&gt;Regarding the default object, I don't see much point in making that part of the API, because it's so easy to just write your block to handle it:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id foo = MALazyFuture(^{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id obj = [self mayReturnNil];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(!obj)
&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;obj = placeholderValue;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return obj;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;More useful, I think, would be extending the API to take a placeholder &lt;i&gt;class&lt;/i&gt; that could be used to look up method signatures, e.g.:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id foo = MALazyFuture(SomeClass, ^{ return [self mayReturnNil]; });
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;However, this still suffers from the problem of nil checks.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Charles:&lt;/b&gt; I always expect my readers to use their own judgement, of course, but if I don't make the warning then I generally consider it to be safe. The nil thing is an annoying problem, but only if you're writing code which might actually return nil. If you know it can't return nil (and most code has tons of places where an unexpected nil would be fatal anyway) then everything should be safe.
&lt;br /&gt;
&lt;br /&gt;There is the potential for trouble if you pass futures to code that is written in such a way as to not deal properly with proxies (like the one my &lt;code&gt;-class&lt;/code&gt; override hack works around) but I expect that sort of thing to be extremely rare, and could be considered a bug in Cocoa, depending on the exact circumstances.</description><guid isPermaLink="true">d76016fcbd11d336168335c8e99c5d11</guid><pubDate>Fri, 26 Feb 2010 21:10:07 GMT</pubDate></item><item><title>Charles Parnot - 2010-02-26 20:46:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>Thanks Mike, this is an awesome post: you designed a very powerful tool with, when you look at it, a very small amount of code.
&lt;br /&gt;
&lt;br /&gt;In other posts with other interesting constructs you made, you have sometimes warned against not using them in "production code". What would be your advice with your futures implementation?
&lt;br /&gt;
&lt;br /&gt;It seems the main concern is the 'nil' value. Remy's suggestion solves both issues of method signature and messaging to nil. But it can often only push the problem further away: the code that relies on checking for nil (which may not even be your code) may not crash or raise an exception, but will still be handling an unexpected result without realizing it, and it is likely to result in other problems down the road. Failing early might be a better option, and this can be done within MABaseFuture.</description><guid isPermaLink="true">ae8961295c027516a46de450845a50f2</guid><pubDate>Fri, 26 Feb 2010 20:46:53 GMT</pubDate></item><item><title>Remy "Psy" Demarest - 2010-02-26 20:34:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>Hi, I would like to know why in your implementations of retain and release you never use Foundation NSIncrementExtraRefCount() and NSDecrementExtraRefCountWasZero() functions for reference counting ? Cocoa uses them and don't seem to have much problem with threading as far as I can see.
&lt;br /&gt;
&lt;br /&gt;Also, for the problem of nil resolved values, wouldn't it be possible to use some kind of default value ? You add a parameter to your method that allows the implementor to provide a value (for example if an NSString is required you put a @"" as default value) so in case the resolved value is nil it will return that object instead.
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;On a side note, when you use an instance of NSString class itself (the abstract class, which is illegal), the abstract methods throw an exception like that if that interests you :D :
&lt;br /&gt;@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** -%s cannot be sent to an abstract object of class %@: Create a concrete instance!", sel_getName(_cmd), [self class]] userInfo:nil];</description><guid isPermaLink="true">142dd6884dd8d0f85750b0026a64bd2b</guid><pubDate>Fri, 26 Feb 2010 20:34:56 GMT</pubDate></item><item><title>John McLaughlin - 2010-02-26 20:26:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-02-26-futures.html#comments</link><description>Thanks Mike,
&lt;br /&gt;
&lt;br /&gt;As usual a very interesting article that I've already begun to think of how to use Futures in code I'm working on today!
&lt;br /&gt;
&lt;br /&gt;-john</description><guid isPermaLink="true">e890716a6fa03c93dfc0860c3739d4a2</guid><pubDate>Fri, 26 Feb 2010 20:26:45 GMT</pubDate></item></channel></rss>
