It should be possible to decorate instancetype with protocols

Originator:jonsterling
Number:rdar://10849187 Date Originated:11-Feb-2012
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:Always
 
Summary:

instancetype is a keyword to allow return-type covariance in Objective-C. It cannot, however, be decorated with protocols.

Steps to Reproduce:

@protocol Decorated
- (instancetype <Decorated>)operation:(id)operand;
@end
 

Expected Results:

For every class T which implements <Decorated>, -[T operation:] should return a value of type `T <Decorated>*`. At first glance, this may not seem that useful. However, in cases where we do not wish the interface of a protocol to pollute all objects in all contexts (even though it is implemented universally), we may wish to return objects that are promised to implement a protocol.

Currently, we have two paths that we can follow. Lose covariance (and thus safety):

@protocol Decorated
- (id <Decorated>)operation:(id)operand;
@end

or require more casting upfront for API consumers:

@protocol Decorated
- (instancetype)operation:(id)operand;
@end

void example(NSString <Decorated> *input) {
  [(NSString<Decorated>*)[input operation:@"hi"] operation:operand];
}

No bueno.


Actual Results:

Parse error: “Expected ')'”.

Comments

+1 great feature for objc-c

By ndarmancev at Sept. 19, 2017, 7:45 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!