Skip to content

Instantly share code, notes, and snippets.

@nsforge
Created December 20, 2012 03:15
Show Gist options
  • Save nsforge/4342663 to your computer and use it in GitHub Desktop.
Save nsforge/4342663 to your computer and use it in GitHub Desktop.
A simple example of how Objective-C's instancetype would be useful in method arguments
@interface MyRequest : NSObject
// Variation One
- (void)startRequestWithCompletionBlock1:(void (^)(MyRequest *request))completionBlock;
// Variation Two
- (void)startRequestWithCompletionBlock2:(void (^)(id request))completionBlock;
// Variation Three
- (void)startRequestWithCompletionBlock3:(void (^)(instancetype request))completionBlock;
@end
...
@interface MyRequestSubclass : MyRequest
- (void)aSubclassMethod;
@end
...
// Usage
MyRequestSubclass *request;
[request startRequestWithCompletionBlock1:^(MyRequest *request){
MyRequestSubclass *requestAsSubclass = (MyRequestSubclass *)request; // typecast from MyRequest* to MyRequestSubclass* loses type safety
[requestAsSubclass aSubclassMethod];
}];
[request startRequestWithCompletionBlock2:^(MyRequestSubclass *request){ // typecast from id to MyRequestSubclass* loses type safety
[request aSubclassMethod];
}];
[request startRequestWithCompletionBlock3:^(instancetype request){ // typesafe
[request aSubclassMethod];
}];
@tkremenek
Copy link

The bottom up expression parsing and type checking is fundamental to the language itself. There are some cases where we can add limited type inference because it happens to work within the limitations of the language, e.g. instancetype or auto in C++11, but those are very limited cases. If you look at a language where type inference is a fundamental component, e.g. OCaml, the language parsing rules and type system are very different. In such a language, the type information can flow both up and down an expression tree, while in C this isn't possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment