Re: Mutable and Immutable Designs
Re: Mutable and Immutable Designs
- Subject: Re: Mutable and Immutable Designs
- From: Wade Tregaskis <email@hidden>
- Date: Sat, 5 Jun 2004 09:30:42 +1000
This seems to work fine, but my question is whether im doing the right
thing when in my mutable class i redefine copyWithZOne: like this:
- (id)copyWithZone:(NSZone *)aZone
{
return [self mutableCopyWithZone: aZone];
}
It seems to make sense.
It does, but I'm pretty sure the convention is for copyWithZone to
return an immutable version, where such a thing exists. That's why
mutableCopyWithZone exists... I guess the theory is mutable copies
might be more expensive, so you provide an immutable copy even on a
mutable class for cases where you don't need that extra functionality
(e.g. if you're just archiving things, or snapshoting, or whatever).
Also, What happens when i make a subclass? All my subclasses must
necessarily be mutable, since i cant subclass from my immutable one
then make a mutable version without redefining everything in my
mutable class it would seem.
Well, yeah - you can make immutable's subclasses of your mutable, but
that creates a few issues, not the least of which being what happens
when someone extends your mutable version with a category, which will
then magically work on your immutable version as well - probably not a
good idea. :)
Whether you have two peer classes or a mutable subclass is entirely up
to you - I'd say subclassing is generally a better idea, as your
mutable then inherits any additions to the immutable. If you don't
want that, go the peer classes route. Either way you can selectively
subclass either immutable or mutable versions of the class.
One simple problem with doing a subclass version is that I've seen
people try to return self cast to the super class in various methods
(like copy), which obviously means nothing to the runtime system and
doesn't provide the necessary guarantee's for immutability.
Wade Tregaskis (aim: wadetregaskis)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.