views:

86

answers:

1

I'm trying to learn and use Blocks effectively. On the web, I've come across this tasty morsel of code:

long long (^blockFun)() = (long long (^)())moreBlockFun;

it confuses the heck out of me. I think it's trying to create a block that expects a block that returns a long and i think it's doing some casting somewhere too, just for fun.

can someone explain this to me?

+6  A: 

It's a block type cast and yes, the syntax isn't great. We assume that moreBlockFun is a block that takes no parameters, and returns something with a sensible cast to long long - this type signature is written long long (^)(). So we create a new local name for that block called blockFun, with the syntax long long (^blockFun)(), and perform the cast.

It's a mess mostly inherited from function pointer type notation, which virtually every C programmer has to lookup around 482 times before they remember it. You're not alone!

Adam Wright
ah... awesome. makes sense now. is there some syntactical sugar one could sprinkle on all this to make it more readable?
pxl
You can (and should) introduce `typedef`s so you can write "FooBlock" or similar to save yourself the main of using the full signature at all points.
Adam Wright
thanks, will do
pxl
(1) Use typedefs. It adds sanity to the insane pointer syntax. (2) When creating the Blocks syntax, I realized that it made understanding function pointers much easier. Basically, take any block declaration, replace the ^ with * and you have the equivalent function pointer! :)
bbum