> On Mar 11, 2015, at 2:21 AM, Jonas Maebe <[email protected]> wrote: > Hi, > > I'm wondering what the "official way" is that a compiler is supposed to > use to disambiguate the following code: > > NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: path]; > NSArray *arr = [[NSArray alloc] initWithContentsOfFile: path]; > > Since alloc returns an id, in theory both NSArray's and NSDictionary's > initWithContentsOfFile could be called by either statement. > > In order to determine whether the initWithContentsOfFile returns an > NSDictionary* or an NSArray*, should the compiler check explicitly > whether this message is sent to the result of a class message (or a > class method called "alloc"), and if so use the class type of its > target? And if that class type does not have any initWithContentsOfFile > instance method, only then fall back to general "id"-based searching? Or > is there a more general/different rule?
In this case, +alloc returns instancetype, and the compiler is clever enough to know how to interpret that for direct class method invocations, with the result that [NSDictionary alloc] has type NSDictionary*. This is a relatively new advance, though: two years ago, maybe? The compiler is generally flummoxed by non-direct class method invocations and just falls back on id-like searching, but there are some other special cases / hacks in play, some more principled than others. The compiler generally only does id-based searching when messaging id or Class (and only without protocol qualifiers). In either case, if it has more precise type information, it expects that type to provide that method. John. _______________________________________________ Do not post admin requests to the list. They will be ignored. Objc-language mailing list ([email protected]) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com This email sent to [email protected]
