Re: const correctness
Re: const correctness
- Subject: Re: const correctness
- From: Ian Joyner <email@hidden>
- Date: Tue, 20 Mar 2012 12:52:33 +1100
On 20 Mar 2012, at 02:43, Nick Zitzmann wrote:
> On Mar 19, 2012, at 5:51 AM, Ian Joyner wrote:
>
>> Actually NSArray. Note NSMutableArray inherits from NSArray because it adds extra functionality in methods that can change the object. You can assign an NSMutableArray object to an NSArray reference, thereby making it unchangeable via that path, but you can't assign an NSArray object to an NSMutableArray.
>
> Declaring an NSMutableArray as an NSArray doesn't make it immutable, because you can still send NSMutableArray messages to the array and they'll still work (though the compiler will complain unless you mask them or send the message indirectly). The only way to make a mutable array immutable is to make an immutable copy of the array.
I think we agree. The point is that we have two types involved, the type of the object itself and the type of the pathway to it (reference). That's why you should not (and cannot I hope) assign an NSArray (immutable) object to an NSMutableArray. The general case is that you cannot assign an object of a supertype to a reference to a subtype, because then you can do inappropriate things to the object.
The interesting case is when you have a supertype reference to an object of a subtype and then try assigning that object back to a reference of the same subtype. This is a legitimate case, but type systems would usually preclude it, which is why those who like dynamic typing don't like static typing. But this case can be handled in a typed language. Eiffel calls it the assignment attempt, which happens at run time by ensuring that the actual run time type of an object conforms to the type of the reference, else the reference is just set to nothing.
Ian
_______________________________________________
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