Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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