Common Lisp Object System

The Common Lisp Object System is Common Lisp’s object-oriented programming system. It was originally implemented as macros over Common Lisp, but merged into the languge as it was standarized.

Benefits

According to Peter Norvig, with CLOS “16 of 23 patterns have qualitatively simpler implementation in Lisp or Dylan than in C++”1. The circle-ellipse problem, for example, is solved immediately because objects can change class.

Examples

Simple Example

(defclass <ship> ()
  ((name :reader name   ;; Only reader is implemented, so you can't rename a
                        ;; ship
         :initarg :name
         :type string)
   (kind :accessor kind
         :initarg :kind)
   (tonnage :accessor tonnage
            :initarg :tonnage
            :type float)))

(defparameter *my-ship*
              (make-instance '<ship> :name "USS Walter Mondale"
                                     :kind :laundry-ship
                                     :tonnage 345))

(class-of *my-ship*) ;; => #<standard-class <ship>>

Generic Functions

Metaobject Protocol

The Metaobject Protocol (MOP) provides mechanisms for extending CLOS by customizing its behaviour on certain classes. Basically, the MOP sees a class as being an instance of a metaclass (Think of it as a kind of ‘higher-order’ object system, or a ‘two tier’ object system).

References