On Nov 19, 2010, at 10:29 AM, jonat...@mugginsoft.com 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 (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 arch...@mail-archive.com

Reply via email to