Re: readonly property which is a mutable array
Re: readonly property which is a mutable array
- Subject: Re: readonly property which is a mutable array
- From: Andreas Grosam <email@hidden>
- Date: Fri, 19 Nov 2010 13:35:54 +0100
On Nov 19, 2010, at 10:29 AM, email@hidden wrote:
> If I receive an NSArray instance from a method I can for example identify the index of a particular object within the array, say at idx, safe in the knowledge that my object is at the given index.
>
> If however the object I receive is an NSMutableArray instance then the assumption is false as the array may get mutated beneath me.
> That is unless it can be relied upon that the NSMutableArray instance won't get mutated.
In my humble opinion (and I guess, I'm not alone), immutable objects shall be immutable. So, if you receive an immutable object we should rely on it that its state never (NEVER) changes. From this, we can conclude that sub-classing a mutable class from a immutable class will break this requirement.
In fact this rule has a name: "The Liskov Substitution Principle" (LSP)
You can read more on this fundamental principle here:
<http://en.wikipedia.org/wiki/Liskov_substitution_principle>
So, "Mutable classes" being subclasses of "immutable" classes violates important principles in object-oriented programming. This is the reason why you experience these problems.
Uhm, and Cocoa?
Well, doing it the other way around (mutable inherits from immutable) wasn't quit that easy to implement as well. And frankly, either way would violate the LSP: If you fulfill the immutable guarantee and try to provide an interface for both, you would experience that neither can be a subtype of the other and simultaneously respecting LSP. Nonetheless, it makes sense to have immutable and mutable objects. Their classes however need to be in different branches in the hierarchy.
Regards
Andreas_______________________________________________
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