Re: Recursive blocks
Re: Recursive blocks
- Subject: Re: Recursive blocks
- From: Chris Suter <email@hidden>
- Date: Thu, 27 Jan 2011 17:19:02 +1100
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.
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.
Essentially writing a recursive function involves naming the private code that is recursive (in order to recurse). With blocks this naming can be hidden as an implementation detail behind the public function wrapping it.
I can't really see it as being a big advantage over having a static function.
Anyway, this isn't really relevant to Xcode so any further discussion ought to be off-list.
Kind regards,
Chris
_______________________________________________
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