Skip navigation.
 
mlRe: Garbage collector vs variable lifetime
FROM : Chris Hanson
DATE : Mon Jun 09 20:02:16 2008

On Jun 9, 2008, at 10:24 AM, John Engelhart wrote:

>> This is not a bug.  This is fundamental to how object-oriented 
>> programming works!  You should always be able to pass an instance 
>> of a subclass wherever an instance of the superclass is expected.

>
> You're mistaken.  You have statically typed the object.


John, it is you who are mistaken.  You're mistaken about what 
"statically typed" means in Objective-C.  It does not mean "I will 
always pass an instance of exactly NSArray", it means "I will always 
pass an instance of NSArray or a subclass."

That's all it means.

> Just as '+(NSArray *)array' does not mean "and any possible 
> subclasses"


In fact, it WOULD mean that.  It means "returns an instance of 
NSArray, or any subclass thereof, that senders should treat as an 
NSArray."

For example, if -[NSArray array] were declared as above, it could 
still perfectly validly return a shared instance of a special 
_NSEmptyArray subclass of NSArray.

All your code will know or care about is that what it gets back is 
usable as an NSArray.

>>> If one applies the 'attribute only applies to the class it was 
>>> specified for' rule:
>>>
>>> By statically typing the class to NSArray, you have certified to 
>>> the compiler that no other object type will be received as an 
>>> argument.  When you passed it an object of a different type, even 
>>> a subclass, you broke your promise to the compiler.

>>
>> This is simply wrong.

>
> You're encouraged to read the section "Enabling Static Behavior - 
> Static Typing" in the Objective-C manual.  This is what allows for 
> subclasses to return different types for the same method.  By 
> statically typing a declaration as NSArray, you've told the compiler 
> that the object is a NSArray (only) class object, and only the 
> methods for NSArray apply.


John, with all due respect, I do know what I'm talking about here -- 
I've been working with Objective-C on NeXT and Mac OS X for 11 years 
now -- and it is you who are wrong about this.

Whether you're dealing with Objective-C, C++, or Java, instances of 
subclasses are always substitutable for instances of their superclass 
where the type system is concerned.

> The fact that object instantiation methods return 'id' and not the 
> base class are further evidence that this is indeed the case.


The reason these kinds of methods have a return type of (id) is that 
there is no way to say "returns an object of the receiver's class." 
For example, +[NSArray array] returns (id) rather than (NSArray *) 
because otherwise +[NSMutableArray array] would require a separate 
declaration (NSMutableArray *).  Rather than have a large number of 
separate declarations, these methods return (id).

> You also can't send messages declared in a subclass of NSArray to a 
> 'NSArray *array;' object without getting a warning.  Again, further 
> evidence that the compiler believes that only the explicitly named 
> class is applicable, and not 'any and all subclasses'.


You misunderstand.  You can *assign* an instance of any subclass to a 
variable typed as a class.  The compiler will warn you about any 
messages you send that aren't in the interface of the typed class, 
however.

Thus you can say, perfectly validly:

  NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
  NSArray *array = mutableArray;

This is perfectly fine.  However, the compiler will give you a warning 
if you try to send any non-NSArray messages to "array."

  -- Chris

Related mailsAuthorDate
mlGarbage collector vs variable lifetime Quincey Morris Jun 7, 00:23
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 00:48
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 01:18
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 01:24
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 01:27
mlRe: Garbage collector vs variable lifetime Nick Zitzmann Jun 7, 01:30
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 01:31
mlRe: Garbage collector vs variable lifetime Shawn Erickson Jun 7, 01:33
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 01:42
mlRe: Garbage collector vs variable lifetime Clark Cox Jun 7, 01:55
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 02:05
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 02:36
mlRe: Garbage collector vs variable lifetime George Stuart Jun 7, 02:51
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 7, 05:48
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 05:49
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 06:16
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 07:03
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 7, 09:07
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 10:32
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 10:41
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 13:13
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 13:14
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 13:34
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:29
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:35
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:47
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 15:18
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 15:59
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 16:31
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 7, 17:03
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 20:19
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:30
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 7, 20:34
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:35
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:45
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 7, 21:07
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 21:43
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 21:43
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:24
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:37
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:51
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 8, 01:11
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 8, 02:15
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 8, 02:23
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 8, 04:10
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 8, 08:24
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 09:40
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 09:47
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 10:05
mlRe: Garbage collector vs variable lifetime Paul Sargent Jun 8, 12:23
mlRe: Garbage collector vs variable lifetime Chris Kane Jun 9, 01:53
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 02:39
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 05:48
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 9, 07:55
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 09:56
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 9, 10:11
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 12:33
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 12:51
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 9, 13:54
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 9, 18:37
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 19:24
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 20:02
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 9, 20:17
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 20:39
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 10, 00:55
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 10, 17:19
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 10, 17:28
mlRe: Garbage collector vs variable lifetime Adam R. Maxwell Jun 10, 18:18
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 10, 22:17
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 11, 09:01
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 11, 09:15
mlRe: Garbage collector vs variable lifetime Jim Puls Jun 11, 09:29
mlRe: Garbage collector vs variable lifetime Graham Cox Jun 11, 09:35
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 11, 09:41
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 11, 11:06
mlRe: Garbage collector vs variable lifetime Andy Lee Jun 11, 14:42
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 11, 16:55
mlRe: Garbage collector vs variable lifetime Clark Cox Jun 11, 17:45
mlRe: Garbage collector vs variable lifetime j o a r Jun 11, 17:49
ml[moderator] Re: Garbage collector vs variable lifetime Scott Anguish Jun 11, 17:51