in reply to OO Identity Crisis

Howdy!

Another way to look at the problem is "how do I find a given node in the collection?"

If the items in the collection are unblessed scalars, you can compare them directly. The more "interesting" case is where the items are blessed scalars (or "objects"). The default comparison is "==". If they come up with the same address, they are the same thing. If ref(x) ne ref(y), the items are probably not equal, but your situation may permit objects of different classes to still come up "equal". Any deeper comparison *must* (as you note) be a method on the object.

It's already been noted that Java provides an "equals" method in the uber-superclass "Object", so you can always invoke .equals on an object. Something along those lines for your node class(es) would be indicated. Pick an appropriate name for the method and go forth.

is_duplicate could be a wrapper around a find() method that returns the given object if it is in the collection, or some other useful value. find() could be responsible for iterating over the collection in an appropriate manner, applying the comparison function.

yours,
Michael

Replies are listed 'Best First'.
Re^2: OO Identity Crisis
by snoopy (Curate) on Jun 21, 2005 at 07:00 UTC
    >> If the items in the collection are unblessed scalars, you can compare them
    >> directly. The more "interesting" case is where the items are blessed scalars
    >> (or "objects"). The default comparison is "==".

    Class::DBI lets you do a simple "eq" test for sameness of database objects.

    It uses overload to return an encoded key value in a string context, so that $a eq $b works anywhere that primary keys have been declared and both are from the same table.

    Update: This overloading leads to a trivial Schwartzian Transform: @t = map { [$_, "$_"] } @u, potentially making any sorting or duplicate checking lightening fast!!