Subject: Re: Can I overload equal?
From: Erik Naggum <clerik@naggum.no>
Date: 1998/06/27
Newsgroups: comp.lang.lisp
Message-ID: <3107939654267698@naggum.no>


* ee95152@ee.iitm.ernet.in
| Hi, In CLOS, can I overload the function equal so that it can compare two
| objects?  ...  Or should I write some object specific function, just like
| for a structure?

  actually, both answers should be "yes", despite the also correct answer
  "no" to the first question -- you just have to do it somewhat differently
  from what the question implies you would do it, and probably from what
  most people would recommend.  however, to show that it can be done,
  here's a snippet of code that should illustrate the point.

  the key is to use the package system so you effectively define a _new_
  function that is known as EQUAL in your package, but which differs from
  the EQUAL function that is in the COMMON-LISP package.

(defpackage :mani
  (:use :common-lisp)			;and whatever else you need
  (:shadow 'equal))

(in-package :mani)

(defclass mani-class ...whatever...)

(defgeneric equal (object1 object2)
  "true if OBJECT1 and OBJECT2 are in some sense the same.")

(defmethod equal ((object1 t) (object2 t))
  "the standard equality predicate"
  (cl:equal object1 object2))

(defmethod equal ((object1 mani-class) (object2 mani-class))
  "the specialized predicate for MANI-CLASS objects"
  ...whatever...)

  at this point, EQUAL will work like you would expect it to work, but in
  some cases, you may get confusing results.  e.g., #'EQUAL is no longer EQ
  to #'CL:EQUAL, and some functions may test for the functional value of
  the EQUAL symbol in the COMMON-LISP package explicitly.  this may alone
  be sufficient reason _not_ to recommend this way of solving the problem.

#:Erik
-- 
  http://www.naggum.no/spam.html is about my spam protection scheme and how
  to guarantee that you reach me.  in brief: if you reply to a news article
  of mine, be sure to include an In-Reply-To or References header with the
  message-ID of that message in it.  otherwise, you need to read that page.