Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Created May 5, 2014 22:07
Show Gist options
  • Save oleksii-demedetskyi/a552acb505ad07105acb to your computer and use it in GitHub Desktop.
Save oleksii-demedetskyi/a552acb505ad07105acb to your computer and use it in GitHub Desktop.
Example trait usage.
#import <Foundation/Foundation.h>
#define trait(NAME) protocol NAME;\
@interface id_##NAME @end \
typedef id_##NAME<NAME> NAME; \
@protocol NAME
#define trait_implementation(NAME) \
interface NAME##_impl : NSObject<NAME>\
- (NAME *) asTrait$;\
@end \
@implementation NAME##_impl \
- (NAME *) asTrait$ { return (NAME*)self; }\
@trait(SomeInterface)
@property (nonatomic, retain) NSString* test;
- (void)printTest;
@end
@trait_implementation(SomeInterface)
@synthesize test;
- (void)printTest
{
NSLog(@"Test is: %@", self.test);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
SomeInterface* testObject = [SomeInterface_impl new].asTrait$;
testObject.test = @"Check";
[testObject printTest];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment