in reply to OO Identity Crisis

Java solves this with the Object class, which all objects derive from. It contains the "equals" method which returns the address of the object (IIRC) by default and can be overloaded by the child.

While this is great for Java, obviously there's no such thing in Perl. However, you may want to just pick a method name and stick with it (a la "equals") and wrap it in an eval. If it bombs, just compare the addresses by default.

Replies are listed 'Best First'.
Re^2: OO Identity Crisis
by BUU (Prior) on Jun 20, 2005 at 17:29 UTC
    What do you mean "obviously there's no such thing in Perl."? What about the UNIVERSAL class, which every class implicitly inherits from?
      You forgot the context:
      Java solves this with the Object class, which all objects derive from. It contains the "equals" method which returns the address of the object (IIRC) by default and can be overloaded by the child.


      There's no standard definition for "equals" which is what I was referring to. Yes, you are correct, though, an equals method could be introduced into UNIVERSAL and simply overridden wherever you like, but it's not going to be something you can rely upon unless they're your own objects.
Re^2: OO Identity Crisis
by artist (Parson) on Jun 20, 2005 at 21:04 UTC
    The 'equals' method also requires to compare the given object with each existing objects. Isn't it the case?
    --Artist

      Sort of. If we're using tlm's approach of a hashtable anyway (see return $self->{ N }{ $id } ||= $node;), then --- as far as I know --- Java's Hashtable combines the Object class's overridable methods equals and hashCode to do what we want. Basically, it avoids comparing the given object with each existing object by comparing it only to those with the same hashcode; but if you have a poor hashcode function (like return 0;), it will compare with everything.