The Common Lisp Object System (CLOS) is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object systems such as MIT Flavors and CommonLOOPS, although it is more general than either. Originally proposed as an add-on, CLOS was adopted as part of the ANSI standard for Common Lisp and has been adapted into other Lisp dialects like EuLisp or Emacs Lisp.
The basic building blocks of CLOS are classes, instances of classes, generic functions and their methods. CLOS provides macros to define those: defclass
, defgeneric
and defmethod
. Instances are created with the function make-instance
.
Classes can have multiple superclasses, a list of slots (member variables in C++/Java parlance) and a special meta class. Slots can be allocated by class (all instances of a class share the slot) or by instance. Each slot has a name and the value of a slot can be accessed by that name using the function slot-value
. Additionally special generic functions can be defined to write or read values of slots. Each slot in a CLOS class must have a unique name.
CLOS is a multiple dispatch system. This means that methods can be specialized upon any or all of their required arguments. Most OO languages are single-dispatch, meaning that methods are only specialized on the first argument. Another unusual feature is that methods do not "belong" to classes; classes do not provide a namespace for generic functions or methods. Methods are defined separately from classes, and they have no special access (e.g. "this", "self", or "protected") to class slots.