<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jul 26, 2008, at 1:17 AM, Ken Ferry wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; ">On Sat, Jul 26, 2008 at 12:02 AM, Chris Hanson <<a href="mailto:cmh@me.com">cmh@me.com</a>> wrote:<br><blockquote type="cite">On Jul 25, 2008, at 6:28 PM, Ted Kremenek wrote:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">It is, in fact, an error to return a NSMutableArray in a method<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">that's prototyped to return an NSArray due to C pointer aliasing<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">rules. The 'id' type is the closest thing that Objective-C has to a<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">'generic object pointer type', so if a method wants to return a<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">pointer to an object of more than one type, it really should declare<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">the return type as 'id'.<br></blockquote></blockquote></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">John is also incorrect about the above: It is *not* an error, in<br></blockquote><blockquote type="cite">Objective-C, to return an instance of a subclass from a method<br></blockquote><blockquote type="cite">prototyped as returning an instance of the superclass.<br></blockquote><br>John may be thinking of the fact that most init methods and such<br>return id, for example -[NSArray initWithObjects:]. This isn't<br>because it would be incorrect to return (NSArray *), it's because<br>subclassers would have to redeclare every init method, or else the<br>compiler would issue a warning for lines such as<br><br>NSMutableArray *mutableArray = [[NSMutableArray alloc]<br>initWithObjects:obj1, obj2, nil];</span></blockquote><div><br></div><div>This limitation also exists in Java with its own collection classes (collections use type erasure). Lists, Maps, etc., all contain objects that subclass Object (the root of the Object hierarchy). Clients must use downcasts when retrieving objects from collections. Generics in Java helps reduce the typing for this, but its just syntactic sugar (the compiler inserts the downcast checks):</div><div><br></div><div><a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html</a></div><div><br></div><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; ">Actually, since the +alloc method returns id (for the same reason),<br>the compiler cannot even tell that -[NSMutableArray initWithObjects:]<br>should be preferred over -[NSArray initWithObjects:] here.<br><br>It'd be nice if Objective-C had return types that meant "instance of<br>receiver" and "instance of receiver's class".</span></blockquote><br></div><div>I agree that this kind of polymorphism would be nice to have in Objective-C. In many cases this kind of polymorphism can be implemented in C++ using templates.</div><div><br></div><div><br></div><div><br></div><div><br></div><br></body></html>