4
votes

I am wondering why there is no built-in equality operator in Common Lisp for comparing CLOS objects (standard-classes). For instance, "equalp" can be applied on arrays, structures, hash-tables, however not on objects.

I assume a new test which descends an object and checks if slot-values are equalp can be written by the programmer, but I wonder if there is a reason for this not being part of the standard, since I imagine it's a fairly common thing to do? For instance, it seems the test functions for "make-hash-table" must be one of the built-in ones*, thus, I don't really see how to use objects as keys or values in a hash table.

*I've noticed there is a related question (Using Common Lisp CLOS objects as keys in a hashtable?), however it does not really answer my question.

Thanks and cheers!

M

1
Usually if you're comparing two objects, you either want to check if they are the same object, which you can do with EQ (works with hashtables as well), or you want to check if a specific slot is equal, which naturally requires you to write your own comparison function. - jkiiski
With a bit of MOP you can write generic equality test, say EQUAL? that does EQUALP on everything except classes instances (i.e. STANDARD-OBJECT), and does EQUAL? on all the slots (of course you must check if objects belong to the same class first and slots a bound). Slow, but probably what you want in 99% of cases, - mobiuseng
Whats wrong with just using EQ which Returns true if its arguments are the same, identical object? - Student

1 Answers

3
votes

I cannot offer a definitive answer, but I suspect that a part of the reason is historical: CLOS was added to ANSI CL after the spec for equalp was finalized.

Note that equalp works on structure-objects as you expect it. Note also that structures have a readable print syntax while CLOS objects lack it.

However, this might not be such a big oversight as one might think at first.

CLOS objects can be relatively heavy-weight; given that slot accessors are generic functions, figuring out object equality can quickly devolve to comparing functions.