Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active April 21, 2020 16:51
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtbandes/881f07a955ff2eadd1a0 to your computer and use it in GitHub Desktop.
Save jtbandes/881f07a955ff2eadd1a0 to your computer and use it in GitHub Desktop.
fun with Obj-C "generics"
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface Function<__contravariant InType, __covariant OutType> : NSObject
{
// interestingly, this does work here (though OutType & InType are not available in the @implementation)
OutType (^_block)(InType);
}
- (instancetype)initWithBlock:(OutType (^)(InType))block;
- (OutType)evaluateAt:(InType)value;
@end
NS_ASSUME_NONNULL_END
// Unfortunately, the generated Swift interface is not very useful:
import Foundation
class Function : NSObject {
init(block: (AnyObject) -> AnyObject)
func evaluateAt(value: AnyObject) -> AnyObject
}
@import Foundation;
#import "Function.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Function<NSString *, NSArray<NSString *> *> *stringSplitter = [[Function alloc] initWithBlock:^(NSString *str) {
return [str componentsSeparatedByString:@","];
}];
// works (unspecified)
NSArray *__unused result0 = [stringSplitter evaluateAt:@"hello,there"];
// works (all NSStrings are NSObjects)
NSArray<NSObject *> *__unused result1 = [stringSplitter evaluateAt:@"hello,there"];
// warning: incompatible pointer types ... (not all NSStrings are NSMutableStrings)
NSArray<NSMutableString *> *__unused result2 = [stringSplitter evaluateAt:@"hello,there"];
// works (all NSMutableStrings are NSStrings)
Function<NSMutableString *, NSArray<NSObject *> *> *__unused mutableStringSplitter = stringSplitter;
// warning: incompatible pointer types ... (not all NSObjects are NSStrings)
Function<NSObject *, NSArray<NSString *> *> *__unused arbitrarySplitter = stringSplitter;
// warning: incompatible pointer types ... (not all NSStrings are NSMutableStrings)
Function<NSString *, NSArray<NSMutableString *> *> *__unused stringSplitterToMutable = stringSplitter;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment