Skip to content

Instantly share code, notes, and snippets.

@cesteban
Forked from drodriguez/test-proto.m
Last active August 29, 2015 14:26
Show Gist options
  • Save cesteban/1827fcb6e44f9eb822f2 to your computer and use it in GitHub Desktop.
Save cesteban/1827fcb6e44f9eb822f2 to your computer and use it in GitHub Desktop.
// 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