Last active
December 10, 2015 11:58
-
-
Save nicklockwood/4430920 to your computer and use it in GitHub Desktop.
An evil idea for writing a metalanguage on top of Objective C where the syntax is actually pleasant. Nobody should ever, ever do this.
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
//Interface | |
@interface NSObject (Evil) | |
@property (readonly) BOOL (^equals)(id object); | |
@end | |
@interface NSArray (Evil) | |
@property (readonly) NSArray *(^append)(id object); | |
@end |
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
//Implementation | |
@implementation NSObject (Evil) | |
- (BOOL (^)(id object))equals | |
{ | |
id __weak weakSelf = self; | |
return ^(id object) { | |
return [weakSelf isEqualTo:object]; | |
}; | |
} | |
@end | |
@implementation NSArray(Evil) | |
- (NSArray *(^)(id object))append | |
{ | |
NSArray *__weak weakSelf = self; | |
return ^(id object) { | |
return [weakSelf arrayByAddingObject:object]; | |
}; | |
} | |
@end |
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
//Tests | |
- (void)testAppend | |
{ | |
NSArray *input = @[@"foo"]; | |
NSArray *output = @[@"foo", @"bar"]; | |
NSAssert(input.append(@"bar").equals(output), @"Append didn't work"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment