Re: Best Practice for Returning Immutable Objects?
Re: Best Practice for Returning Immutable Objects?
- Subject: Re: Best Practice for Returning Immutable Objects?
- From: j o a r <email@hidden>
- Date: Tue, 3 Jun 2008 09:15:19 -0700
On Jun 3, 2008, at 8:56 AM, Karl Moskowski wrote:
Would it be sufficient to cast, like this?
:
return (NSData *)myData;
Does this generalize to other non-collection classes, e.g., NSString?
In the general case yes. I would say that it's not up to you to ensure
that this object is not being accidently mutated at some later point -
you already declared the returned object to be of an immutable type.
If someone later needs to ensure that they hold an immutable and
private copy, they can always copy it at that point. If the object
being copied is already immutable, this is typically cheap. See the
"Copying Mutable Versus Immutable Objects" optimization here:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/ImplementCopy.html#//apple_ref/doc/uid/20000049-999791
>
j o a r
_______________________________________________
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