-
-
Save drodriguez/671b668c72018058ffb9 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
// clang -o test-proto -Wall -framework Foundation -fmodules -fobjc-arc test-proto.m && test | |
@import Foundation; | |
@protocol A <NSObject> | |
@property (nonatomic, copy, readonly) NSString *a; | |
@end | |
@protocol B <NSObject> | |
@property (nonatomic, copy, readonly) NSString *b; | |
@end | |
@interface C : NSObject <A> | |
@end | |
@interface D : NSObject <A, B> | |
@end | |
@interface E : NSObject | |
+ (void)doSomethingWithA:(id<A>)a; | |
+ (id<A>)returnA; | |
+ (id<A>)returnADoingSomethingWithA:(id<A>)a; | |
@end | |
int main(int argc, char **argv) { | |
@autoreleasepool { | |
C *c1 = [[C alloc] init]; | |
D *d1 = [[D alloc] init]; | |
[E doSomethingWithA:c1]; // 👍 | |
[E doSomethingWithA:d1]; // 👍 | |
C *c2 = [E returnA]; // 👍 | |
D *d2 = [E returnA]; // ❌ warning: initializing 'D *__strong' with an expression of incompatible type 'id<A>' | |
C *c3 = [E returnADoingSomethingWithA:c1]; // 👍 | |
D *d3 = [E returnADoingSomethingWithA:d1]; // ❌ warning: initializing 'D *__strong' with an expression of incompatible type 'id<A>' | |
C *c4 = [E returnADoingSomethingWithA:d1]; // 👍 | |
D *d4 = [E returnADoingSomethingWithA:c1]; // ❌ warning: initializing 'D *__strong' with an expression of incompatible type 'id<A>' | |
NSLog(@"%@", c1); | |
NSLog(@"%@", d1); | |
NSLog(@"%@", c2); | |
NSLog(@"%@", d2); | |
NSLog(@"%@", c3); | |
NSLog(@"%@", d3); | |
NSLog(@"%@", c4); | |
NSLog(@"%@", d4); | |
} | |
return 0; | |
} | |
@implementation C | |
@synthesize a; | |
@end | |
@implementation D | |
@synthesize a; | |
@synthesize b; | |
@end | |
@implementation E | |
+ (void)doSomethingWithA:(id<A>)a { | |
NSLog(@"%@", a); | |
} | |
+ (id<A>)returnA { | |
return nil; | |
} | |
+ (id<A>)returnADoingSomethingWithA:(id<A>)a { | |
return a; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para que luego os riais de mí cuando digo covarianza y contravarianza, cabrones (pero con cariño).