[cfe-dev] Even more clang ideas

Chris Hanson cmh at me.com
Sat Jul 26 00:02:40 PDT 2008


On Jul 25, 2008, at 6:28 PM, Ted Kremenek wrote:

>> It is, in fact, an error to return a NSMutableArray in a method
>> that's prototyped to return an NSArray due to C pointer aliasing
>> rules. The 'id' type is the closest thing that Objective-C has to a
>> 'generic object pointer type', so if a method wants to return a
>> pointer to an object of more than one type, it really should declare
>> the return type as 'id'.  Again, this is due to the C pointer
>> aliasing rules rather than any OO conceptual rules.
>
> Again, I'm not certain how much C99's aliasing rules apply to
> Objective-C object references.  Objective-C doesn't have a formal
> specification akin to C99, so the specification (if you want to call
> it that) is whatever the current compiler implementation allows.

The current specification of Objective-C is "The Objective-C 2.0  
Programming Language" at <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ 
 >.

John is also incorrect about the above:  It is *not* an error, in  
Objective-C, to return an instance of a subclass from a method  
prototyped as returning an instance of the superclass.

Objective-C is its own language that extends C99, not a preprocessor  
for C99, and this is one of the extensions that Objective-C adds.  In  
fact, in Objective-C it is not possible to say "this method returns an  
instance of specifically this class and no other class" -- you can  
only say "this method returns an instance of this class or any  
subclass."

That is by design, and is not just an artifact of its original  
mid-1980s implementation as a preprocessor.

   -- Chris




More information about the cfe-dev mailing list