Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created June 20, 2014 02:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeash/4fa887db918cd8c16ca1 to your computer and use it in GitHub Desktop.
Save mikeash/4fa887db918cd8c16ca1 to your computer and use it in GitHub Desktop.
#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