Skip to content

Instantly share code, notes, and snippets.

@JensAyton
Created January 5, 2017 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensAyton/fb3d9dacc35e001779ed57f7acb1b011 to your computer and use it in GitHub Desktop.
Save JensAyton/fb3d9dacc35e001779ed57f7acb1b011 to your computer and use it in GitHub Desktop.
Something something named arguments
struct ifparams {
#if __has_feature(objc_arc)
__unsafe_unretained
#endif
dispatch_block_t then, otherwise;
};
static inline void iff(bool condition, struct ifparams params) {
((condition?params.then:params.otherwise)?:^{})();
}
#define if(condition, ...) iff(condition, (struct ifparams){ __VA_ARGS__ })
int main(int argc, const char * argv[]) {
if (5 > 3, .then = ^{
printf("I can't remember why I'm doing this\n");
});
return 0;
}
@JensAyton
Copy link
Author

Oh yea, I forgot #define else otherwise.

@JensAyton
Copy link
Author

And without native control flow:

static inline void iff(bool condition, struct ifparams params) {
	dispatch_block_t c1[] = { params.otherwise, params.then };
	dispatch_block_t c2[] = { ^{}, c1[condition] };
	c2[!!c2[1]]();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment