Skip navigation.
 
mlRe: Best Practice for Returning Immutable Objects?
FROM : Seth Willits
DATE : Tue Jun 03 21:07:09 2008

On Jun 3, 2008, at 9:15 AM, j o a r 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 used to think that. Now I'm converting to making sure it truly is 
returning an immutable object. There have been cases where I've 
changed it under myself and run into problems. I figure if it says 
it's immutable, it'd better be immutable.

That said, because of my own bugs, I've also taken to copying some 
objects (if given as a parameter to a setObject: method, for instance) 
instead of simply retaining them. So I guess I'm moving towards being 
doubly safe.

If I get an object from AppKit or Foundation etc that's an immutable 
object, I assume it to truly be immutable. Maybe someone should write 
an automated tool like Ken Ferry did with alloc/init and figure out 
how many framework calls actually return mutable objects posing as 
immutable objects? :-)


--
Seth Willits

Related mailsAuthorDate
mlBest Practice for Returning Immutable Objects? Karl Moskowski Jun 3, 17:56
mlRe: Best Practice for Returning Immutable Objects? Shawn Erickson Jun 3, 18:08
mlRe: Best Practice for Returning Immutable Objects? Mike Abdullah Jun 3, 18:13
mlRe: Best Practice for Returning Immutable Objects? j o a r Jun 3, 18:15
mlRe: Best Practice for Returning Immutable Objects? Karl Moskowski Jun 3, 18:16
mlRe: Best Practice for Returning Immutable Objects? Bob Warwick - Code… Jun 3, 18:17
mlRe: Best Practice for Returning Immutable Objects? Karl Moskowski Jun 3, 18:20
mlRe: Best Practice for Returning Immutable Objects? Graham.Lee Jun 3, 18:23
mlRe: Best Practice for Returning Immutable Objects? Karl Moskowski Jun 3, 18:28
mlRe: Best Practice for Returning Immutable Objects? Hamish Allan Jun 3, 18:52
mlRe: Best Practice for Returning Immutable Objects? Seth Willits Jun 3, 21:07
mlRe: Best Practice for Returning Immutable Objects? Kyle Sluder Jun 3, 22:25
mlRe: Best Practice for Returning Immutable Objects? Michael Ash Jun 4, 04:33