-
-
Save CraigSiemens/bcdefff3880c508ad2b1 to your computer and use it in GitHub Desktop.
if-let and guard macros for Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// VARIABLE must be a variable declaration (NSString *foo) | |
// VALUE is what you are checking is not nil | |
// WHERE is an additional BOOL condition | |
#define iflet(VARIABLE, VALUE) \ | |
ifletwhere(VARIABLE, VALUE, YES) | |
#define ifletwhere(VARIABLE, VALUE, WHERE) \ | |
for (BOOL b_ = YES; b_ != NO;) \ | |
for (id obj_ = (VALUE); b_ != NO;) \ | |
for (VARIABLE = (obj_ ?: (VALUE)); b_ != NO; b_ = NO) \ | |
if (obj_ != nil && (WHERE)) | |
// Called just like the swift verstion | |
// guard(1 < 2) else { | |
// return | |
// } | |
#define guard(CONDITION) \ | |
if (CONDITION) {} | |
#define guardletwhere(VARIABLE, VALUE, WHERE) \ | |
ifletwhere(VARIABLE, VALUE, WHERE) {} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSString *x = @"asdf"; | |
NSString *y = nil; | |
iflet(NSString *z, x) { | |
NSLog(@"x is not nil"); | |
} | |
iflet(NSString *z, y) { | |
NSLog(@"y is not nil"); | |
} | |
else { | |
NSLog(@"y is nil"); | |
} | |
ifletwhere(NSString *z, x, z.length < 100) { | |
NSLog(@"x is not nil and < 100"); | |
} | |
guard(3 < 2) else { | |
NSLog(@"guard failed"); | |
} | |
guardletwhere(NSString *z, x, z.length < 3) else { | |
NSLog(@"guardletwhere failed"); | |
} | |
} | |
} | |
/* outputs: | |
iflet[57328:5182484] x is not nil | |
iflet[57328:5182484] y is nil | |
iflet[57328:5182484] x is not nil and < 100 | |
iflet[57328:5182484] guard failed | |
iflet[57328:5182484] guardletwhere failed | |
*/ |
There is an issue when calling 'continue' in these macros, as they are in for loop without brackets
What difference would it make if they were in a loop with brackets? I see no difference in behavior.
suggesting change to
Yet this approach has two other drawbacks:
VARIABLE
will be available outside of theiflet
block, which does not matchif let
behavior in Swift. Also you cannot use the same variable for twoiflet
blocks in a row as the compiler will throw a re-declaration error but this is also possible in Swift.- You cannot use
iflet
as an else statement (else iflet (...)
) or after anotherif
(if (...) iflet(...)
). Both will not create a compile error but the code won't do what you expect it to do.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
suggesting change to