Created
June 20, 2014 02:40
-
-
Save mikeash/4fa887db918cd8c16ca1 to your computer and use it in GitHub Desktop.
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> | |
#define GENERIC(type, parameterType) __typeof__(type (^)(parameterType)) | |
#define GENERIC_DERIVED_TYPE(genericObject) __typeof__(genericObject(NULL)) | |
@interface MAList : NSObject | |
- (id)initWithArray: (NSArray *)array; | |
- (id)get: (NSUInteger)index; | |
@end | |
@implementation MAList { | |
NSArray *_array; | |
} | |
- (id)initWithArray: (NSArray *)array { | |
_array = [array copy]; | |
return self; | |
} | |
- (id)get: (NSUInteger)index { | |
return _array[index]; | |
} | |
@end | |
#define MAList(type) GENERIC(type, void *) | |
#define MAListGet(list, index) (GENERIC_DERIVED_TYPE(list))[list get: index] | |
int main(int argc, char **argv) { | |
@autoreleasepool { | |
MAList(NSString *) list = (MAList(NSString *))[[MAList alloc] initWithArray: @[ @"hello", @"world" ]]; | |
NSString *first = MAListGet(list, 0); | |
NSNumber *second = MAListGet(list, 1); | |
NSLog(@"hi %@", list); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment