Search code examples
objective-cnestedblock

Objective-C: Type signature of blocks with block return types


I'm having trouble writing a nested block. Say I want a block that takes and integer. That block returns a block that takes another integer, and returns the sum of the two integers. I haven't had any luck writing this out. Here's one try, which is no worse than any other of mine:

(int ^(int)) (^bblock)(int) = ^(int a) {
    return ^(int b){ return a + b; };
};

Can anybody spot what's wrong?


Solution

  • Quite ugly, but you can do it using parenthesis instead of typedefs:

    int (^(^functor)(int))(int) = ^(int a) {
        return Block_copy(^(int b) {
            return a + b;
        });
    };