On Sep 1, 2013, at 6:36 PM, Kyle Sluder wrote:

> On Sep 1, 2013, at 1:41 PM, Greg Parker <gpar...@apple.com> wrote:
> 
>> You're not entirely left up to their whim. A return value whose declared 
>> type is immutable imposes restrictions on both sides:
>> 1. You must not mutate the returned object.
>> 2. The returned object will not be mutated behind your back by anyone else.
> 
> Sadly, the frameworks don't always live up to their end of the bargain on 
> this one.
> 
> Sometimes their failure to do so is documented—see -[NSTextStorage string], 
> for instance. But usually it’s not.

The docs I linked to earlier strongly imply that there's no such restriction on 
the side which vends a mutable object through an immutable-typed reference.
https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ObjectMutability/ObjectMutability.html#//apple_ref/doc/uid/TP40010810-CH5-SW65

It says things like:

> • If you have a mutable collection that is frequently changed and that you 
> frequently hand out to clients (that is, you return it directly in a getter 
> accessor method), you run the risk of mutating something that your clients 
> might have a reference to. If this risk is probable, the instance variable 
> should be immutable.

That last sentence suggests the "restriction" is a good idea but not absolute.

The example given using -[NSView subviews] makes little sense if the 
restriction were really a restriction:

> • You ask NSView for its subviews (with the subviews method) and it returns 
> an object that is declared to be an NSArray but which could be an 
> NSMutableArray internally. Then you pass that array to some other code that, 
> through introspection, determines it to be mutable and changes it. By 
> changing this array, the code is mutating internal data structures of the 
> NSView class.

It's ostensibly about the caller mutating the internal data structure of 
NSView, but its implication is that the caller has a reference to NSView's 
internal data structure that NSView itself might modify at any time.

And most damning is the whole subsection titled "Make Snapshots of Received 
Objects" which outright states that objects received as immutable may change 
out from under you and puts the onus on you to deal with that:

> If you want to ensure that a supposedly immutable object received from a 
> method does not mutate without your knowing about it, you can make snapshots 
> of the object by copying it locally.

Regards,
Ken


_______________________________________________

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to