Re: Garbage collector vs variable lifetime
Re: Garbage collector vs variable lifetime
- Subject: Re: Garbage collector vs variable lifetime
- From: Jean-Daniel Dupas <email@hidden>
- Date: Wed, 11 Jun 2008 09:41:35 +0200
Le 11 juin 08 à 09:01, John Engelhart a écrit :
On Jun 10, 2008, at 11:28 AM, Charles Srstka wrote:
I think the problem is that if NSArray has +[NSArray array]
returning an NSArray, then NSMutableArray has to return an NSArray
also, since it can't have a different method signature for the same
method. As a result, if you called +[NSMutableArray array], the
compiler would think you were getting a regular, non-mutable
NSArray, and you'd get a warning if you tried to do this, although
the code would still work:
No, each class is allowed to define different types for the same
method:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_9_section_2.html#/
/apple_ref/doc/uid/TP30001163-CH16-TPXREF161
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'.
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.
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. So
when the returned array mutates, either because you were sloppy and
returned the pointer to your objects internal state, or because
during its travels something, somewhere sends it a mutation message
inside a @try / @catch block, I'm going to file a bug because
whatever it is you returned did not behave like a NSArray. An /
immutable/ array.
You have to fill a bug againt the library that mutates the NSArray,
not the one that create it. If a method expects an NSArray, it have to
use it like an NSArray and not like an NSMutableArray.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden