CLOS hint #1

January 26, 2009

Keene’s `Object-oriented programming in Common Lisp’ is the book generally recommended for starting with CLOS. While it’s a very good book and I too recommend it, it was written before the Common Lisp standardization process was finished. Therefore it has some inconsistencies (but so few that you don’t actually have to worry about them). One of them is specializing describe on page 40 (and 41, 49, 56 and 241):


(defmethod describe ((l lock))
  (format t "~&~S is a lock of type ~S named ~A."
          l (type-of l)
          (if (slot-boundp l 'name)
              (lock-name l)
              "(no name)"))
  (values))

This won’t work, since describe is an ordinary function, so you can’t specialize it. You have to specialize describe-object instead, which takes a stream as its 2nd argument. Apart from that it’s the same:


(defmethod describe-object ((l lock) stream)
  (format stream "~&~S is a lock of type ~S named ~A."
          l (type-of l)
          (if (slot-boundp l 'name)
              (lock-name l)
              "(no name)"))
  (values))

See the documentation of describe and describe-object.

Again, this is a good book on CLOS and the code examples are clean. Also, I recommend Practical Common Lisp by Peter Seibel, though it’s not a book on object-oriented Common Lisp, but a general introductory book to the world of Common Lisp.

Leave a Reply