-
-
Save cesteban/1827fcb6e44f9eb822f2 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 <A> | |
@property (nonatomic, copy, readonly) NSString *b; | |
@end | |
@interface C : NSObject <A> | |
@end | |
@interface D : NSObject <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