Tuesday, January 22, 2013

When Is id Promoted to instancetype?

Steven Fisher:

This is straight from Clang Language Extensions:

  • the first word is “alloc” or “new”, and the method is a class method, or
  • the first word is “autorelease”, “init”, “retain”, or “self”, and the method is an instance method.

2 Comments

The post asks the question: "Left unsaid is whether it’s better to actually specify instancetype for these rather than rely on automatic promotion of id."

I don't see how the response could be anything but "it's better". The only advantage of `id` would be to allow the client of the class to substitute any kind of class they want for the return type, like making the compiler believe the returned object is an NSDictionary when calling [NSArray array]. And that seems quite twisted. But maybe I am missing other use cases?

@charles I think he just means if you think typing it out looks ugly or you need compatibility with a compiler that doesn’t support it.

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment