If you attempt to run a null block, your program crashes, so you need to wrap it in a check if it's null, like so:
I had a lot of different spots where I needed to do this kind of thing, so I ended up writing this macro:
And you use it like:
My questions are:
Does anyone see a problem with my macro? Is there some way it can be improved? I think it may not work with blocks which take no arguments at all - what's the best way I can modify it to handle that?
Also, as some of you may be aware, I hate macros in C. They're not hygienic and lead to extremely frustrating to debug situations. But I don't think there's a way to write this without a macro... unless someone can think of that I didn't.
Code:
if (block != nil) {
block(arg);
}
I had a lot of different spots where I needed to do this kind of thing, so I ended up writing this macro:
Code:
#define CHECK_NULL_EXEC_BLOCK(BLOCK, ...) if (BLOCK != nil) BLOCK(__VA_ARGS__)
And you use it like:
Code:
CHECK_NULL_EXEC_BLOCK(block, arg);
My questions are:
Does anyone see a problem with my macro? Is there some way it can be improved? I think it may not work with blocks which take no arguments at all - what's the best way I can modify it to handle that?
Also, as some of you may be aware, I hate macros in C. They're not hygienic and lead to extremely frustrating to debug situations. But I don't think there's a way to write this without a macro... unless someone can think of that I didn't.