Skip navigation.
 
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning
FROM : Chris Suter
DATE : Tue Oct 14 11:58:10 2008

On Tue, Oct 14, 2008 at 8:17 PM, Oleg Krupnov <oleg.<email_removed>> wrote:
> You are right, casting from alloc worked, thank you.
>
> Is it the recommended practice to always cast after alloc?
>
> I still have a question in this regard.
>
> If the alloc returns id, then, from the compiler's perspective, there
> can be two solutions: 1) assume that the id type can have each and any
> method and let it be resolved at run time without any compiler warning
> or


This won't work. The compiler emits different code depending upon the
types involved. For example, there are differences depending on
whether the return types are structures, floating point or integer
values.

> 2) assume that the id does not have any methods (except NSObject's)
> and always issue a compiler warning when there is no explicit casting
> to a type.


Well that would be a pain. You'd get warnings everywhere. id, by
definition, can be sent anything.

> What compiler seems to be doing is rather strange: it looks
> through the project, arbitrarily picks up a class with the matching
> message name and issues a warning if the rest of the actual message
> signature is not matching. How would you explain that?


It comes down to the point I made above. It needs to know about the types.

The proper way to solve this, in my opinion, is to allow a way of
specifying that the return type varies depending on the class it's
implemented on i.e. define the alloc method in such a way so that the
compiler knows that -[<class> alloc] returns an object of type
<class>. Something similar could be used for [NSString string] and
[NSMutableString string] which have a similar problem (they return an
id, rather than an NSString or NSMutableString).

-- Chris

Related mailsAuthorDate
ml[Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Oleg Krupnov Oct 14, 10:43
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 11:00
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Oleg Krupnov Oct 14, 11:17
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Roland King Oct 14, 11:47
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 11:52
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 11:58
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 12:13
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 13:11
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 13:31
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Quincey Morris Oct 14, 18:59
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning John Engelhart Oct 15, 12:03
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 15, 12:36
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Jonathan del Strot… Oct 15, 12:50
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Michael Ash Oct 15, 16:17