On Jun 11, 2008, at 12:01 AM, John Engelhart wrote:

This whole business about '(NSArray *) means NSArray AND any of it's subclasses' is the result of sloppy thinking and confusing 'able to' with 'as per spec'.

No, you are wrong. It really is "per spec." That is an intentional capability in both Objective-C and C++: You can return an instance of a class, or any of its subclasses, wherever an instance of that class is specified precisely because classes should be designed to be substitutable in this fashion. This is the essence of polymorphism in object-oriented programming and is summarized by the Liskov substitution principle.

Languages like Smalltalk and Ruby take this even further with "duck typing," where they lack type declaration entirely and an object's interface is defined entirely by its expected behavior.

If you declare a method prototype as '-(NSArray *)resultsArray', then you have explicitly communicated that a NSArray is going to be returned. Not a NSMutableArray. Not 'Jimmies groovy array with red pin stripes'. A NSArray. Period. A NSMutableArray != a NSArray. If you're going to be returning (or accepting) more than a single class, you use id, which clearly communicates your intentions.

This is absolutely, categorically incorrect.

The Objective-C language, for the purposes of type checking, treats assignment, passing and return of subclasses as equivalent to the assignment, passing and return of superclasses.

It does so specifically so instances of a subclass may be assigned, passed and returned wherever an instance of a superclass is required.

A return type of NSArray * means something very explicit. "But all you can expect is an object that behaves like a NSArray, so returning a subclass of NSArray, like NSMutableArray, is perfectly legal!" You're exactly right that all I can expect is an object that behave like a NSArray. /Exactly/ like a NSArray. Not sorta. Not almost. Exactly. Because that's what you explicitly stated.

You are incorrect. It means all you can expect is an object that obeys NSArray's API contract. Note that it has been pointed out, at LEAST once in this thread, that you will NEVER receive an actual NSArray because NSArray is actually a class cluster.



You keep insisting that Objective-C is designed in and behaves in a way in which it simply does not. Please stop.

  -- Chris

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to