Hi Greg,

hope the runtime is being docile. :-)

On May 4, 2009, at 23:24 , Greg Parker wrote:

That's right. In some discussions of object-oriented programming, a distinction is made between "value objects" and "reference objects". Two value objects can be "equal" if they share the same "value", even if they are separately allocated objects with different pointer addresses in memory. For reference objects, "equal" means they have the same pointer address.

Minor nit: the distinction between identity and equality isn't really tied to having value objects, though there is some overlap, and value objects in Cocoa are somewhat ad-hoc. Smalltalk is a little more consistent here, in that it has there is a simple distinction between identity and equality, with = message for equality and == for identity.

NSNumber acts as a value object by this terminology, because NSNumber's -isEqual: and -hash methods operate on the represented number value, ignoring the actual allocation addresses. On the other hand, NSWindow is a reference object; nobody would consider two different NSWindow objects "equal" even if they shared the same name and contents and position on screen.

Actually, the distinction tends to be somewhat different:

- for value objects, when they are equal they are also considered identical, so two instances of the number 2 are considered indistinguishable. In Cocoa-practice, that they are in fact indistinguishable doesn't necessarily hold, though the frameworks pretty much assume that and so they should be treated as such. - for other objects, equality does not imply identity, so two windows could be considered equal if they have the same content, but no-one would consider them to be the same object ( not that -isEqual: is necessarily implemented that way).


NSDictionary uses -isEqual: and -hash everywhere, so the contained objects get to decide whether they act as value objects or reference objects.

Well, this also depends on how they react to copy.

NSWindow's -isEqual: implementation simply compares the pointer addresses and returns that result, while NSNumber's -isEqual: implementation also looks at the represented number values stored the objects.

If it's a true value object, it needs to *only* look at the value, not "also".

A CFDictionary can be configured to treat all of its objects as reference objects, ignoring any -isEqual: implementations. (Such a dictionary would no longer be compatible with NSDictionary, though.)

It would be an IdentityDictionary.

Cheers,

Marcel
_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [email protected]

Reply via email to