Re: Recursive blocks
Re: Recursive blocks
- Subject: Re: Recursive blocks
- From: Bill Bumgarner <email@hidden>
- Date: Thu, 27 Jan 2011 09:51:45 -0800
__block void(^strawberryFields)();
strawberryFields = ^{ strawberryFields(); };
strawberryFields();
Above is the typical recursive idiom.
On Jan 26, 2011, at 10:19 PM, Chris Suter wrote: Hi Glen,
On Thu, Jan 27, 2011 at 4:47 PM, Glen Low <email@hidden> wrote: Apparently you can do recursive blocks, according to Bill Bumgarner.
The "trick" seems to be to declare the block variable with the __block modifier and break up declaration and initialization, this seems to keep both compiler and analyzer happy.
Yes, I did know that. However, I don't like it because I think it's more unreadable. It's also marginally slower.
You have measured a situation where the pattern's marginal slowness actually matters?
A Block_copy() is going to often be at least an order of magnitude (if not more) slower because it ends up causing a malloc(). Given that there will only be one allocation (or one assignment), it is likely that the overhead of either will be in the noise.
Why do this instead of a block calling a recursive function? Same reason why we use blocks -- it's more convenient when you have to capture other local variables from the scope, otherwise the (private) recursive function will have to declare them all as parameters, and (public) wrapper function and block therein will have to copy or forward all the necessary variables to the recursive function.
Obviously, it would depend on the case, but you should never sacrifice readability for convenience. I'd love to see a real world example where you'd use recursive blocks; the factorial example clearly isn't one.
I found the Blocks example to be more readable, but to each his own. I do agree that a real world example would be a more interesting point of discussion. b.bum (reply-to set to email@hidden) |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden