![Method-combination](https://upload.wikimedia.org/wikipedia/commons/4/43/Method-combination.png)
The Common Lisp Object System (CLOS) is the facility for
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
which is part of
ANSI
The American National Standards Institute (ANSI ) is a private non-profit organization that oversees the development of voluntary consensus standards for products, services, processes, systems, and personnel in the United States. The organi ...
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
. CLOS is a powerful
dynamic
Dynamics (from Greek δυναμικός ''dynamikos'' "powerful", from δύναμις ''dynamis'' "power") or dynamic may refer to:
Physics and engineering
* Dynamics (mechanics)
** Aerodynamics, the study of the motion of air
** Analytical dyna ...
object system which differs radically from the OOP facilities found in more
static language
In computer science, static program analysis (or static analysis) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs during their execution.
The term i ...
s such as
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
or
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
. CLOS was inspired by earlier Lisp object systems such as
MIT Flavors and
CommonLoops CommonLoops (the Common Lisp Object-Oriented Programming System; an acronym reminiscent of the earlier Lisp OO system "Loops" for the Interlisp-D system) is an early programming language which extended Common Lisp to include Object-oriented progr ...
, 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 such as
EuLisp
EuLisp is a statically and dynamically scoped Lisp dialect developed by a loose formation of industrial and academic Lisp users and developers from around Europe. The standardizers intended to create a new Lisp "less encumbered by the past" (c ...
or
Emacs Lisp
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Em ...
.
Features
The basic building blocks of CLOS are
method
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
s,
class
Class or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used differentl ...
es, instances of those classes, and
generic function
In computer programming, a generic function is a function defined for polymorphism.
In statically typed languages
In statically typed languages (such as C++ and Java), the term ''generic functions'' refers to a mechanism for ''compile-time pol ...
s.
CLOS provides macros to define those:
defclass
,
defmethod
, and
defgeneric
. Instances are created with the method
make-instance
.
Classes can have multiple
superclasses, a list of slots (member variables in C++/Java parlance) and a special
metaclass
In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriente ...
. 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
Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of ...
system. This means that
method
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
s 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.
Methods in CLOS are grouped into
generic function
In computer programming, a generic function is a function defined for polymorphism.
In statically typed languages
In statically typed languages (such as C++ and Java), the term ''generic functions'' refers to a mechanism for ''compile-time pol ...
s. A generic function is an object which is callable like a function and which associates a collection of methods with a shared name and argument structure, each specialized for different arguments. Since Common Lisp provides non-CLOS classes for structures and built-in data types (numbers, strings, characters, symbols, ...), CLOS dispatch works also with these non-CLOS classes. CLOS also supports dispatch over individual objects (eql specializers). CLOS does not by default support dispatch over all Common Lisp data types (for example dispatch does not work for fully specialized array types or for types introduced by
deftype
). However, most Common Lisp implementations provide a
metaobject protocol which allows generic functions to provide application specific specialization and dispatch rules.
Dispatch in CLOS is also different from most OO languages:
# Given a list of arguments, a list of applicable methods is determined.
# This list is sorted according to the specificity of their parameter specializers.
# Selected methods from this list are then combined into an effective method using the method combination used by the generic function.
# The effective method is then called with the original arguments.
This dispatch mechanism works at runtime. Adding or removing methods thus may lead to changed effective methods (even when the generic function is called with the same arguments) at runtime. Changing the method combination also may lead to different effective methods.
For example,
; Declare the common argument structure prototype.
(defgeneric f (x y))
; Define an implementation for (f integer t), where t matches all types.
(defmethod f ((x integer) y)
1)
(f 1 2.0) => 1
; Define an implementation for (f integer real).
(defmethod f ((x integer) (y real))
2)
(f 1 2.0) => 2 ;Dispatch changed at runtime.
Like the OO systems in most
dynamic languages, CLOS does not enforce
encapsulation. Any slot can be accessed using the
slot-value
function or via (optionally auto-generated)
accessor method In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the priva ...
s. To access it via
slot-value
you have to know the name of the slot. CL programmers use the language's
package facility to declare which functions or data structures are intended for export.
Apart from normal ("primary") methods, there also are
:before
,
:after
, and
:around
"auxiliary" methods. The former two are invoked prior to, or after the primary method, in a particular order based on the class hierarchy. An
:around
method can control whether the primary method is executed at all. Additionally, the programmer can specify whether all possible primary methods along the
class hierarchy
A class hierarchy or inheritance tree in computer science is a classification of object types, denoting objects as the instantiations of classes (class is like a blueprint, the object is what is built from that blueprint) inter-relating the vario ...
should be called or just the one providing the closest match.
The ''Standard Method-Combination'' provides the primary, before, after and around methods explained above. There are other Method-Combinations with other method types. New (both simple and complex) Method-Combinations and method types can be defined.
CLOS allows
multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
. When the default order in which methods are executed in multiple inheritance is not correct, the programmer may resolve the
diamond inheritance problems by specifying the order of method combinations.
CLOS is dynamic, meaning that not only the contents, but also the ''structure'' of its objects can be modified at runtime. CLOS supports changing class definitions on-the-fly (even when instances of the class in question already exist) as well as changing the class membership of a given instance through the
change-class
operator. CLOS also allows one to add, redefine and remove methods at runtime. The
Circle-Ellipse Problem is readily solved in CLOS, and most OOP
design patterns
''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
either disappear or are qualitatively simpler.
CLOS is not a
prototype language: classes must be defined before objects can be instantiated as members of that class.
Metaobject Protocol
Outside of the ANSI Common Lisp standard, there is a widely implemented extension to CLOS called the
Metaobject In computer science, a metaobject is an object that manipulates, creates, describes, or implements objects (including itself). The object that the metaobject pertains to is called the base object. Some information that a metaobject might define incl ...
Protocol (MOP). The MOP defines a standard interface to the underpinnings of the CLOS implementation, treating classes, slot-descriptions, generic-functions and methods themselves as instances of
metaclass
In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriente ...
es, and allows the definition of new metaclasses and the modification of all CLOS behavior. The flexibility of the CLOS MOP prefigures
aspect-oriented programming
In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying t ...
, which was later developed by some of the same engineers, such as
Gregor Kiczales
Gregor Kiczales is an American computer scientist. He is currently a full time professor of computer science at the University of British Columbia in Vancouver, British Columbia, Canada. He is best known for developing the concept of aspect-orien ...
. The MOP defines the behavior of the whole object system by a set of protocols. These are defined in terms of CLOS. Thus it is possible to create new object-systems by extending or changing the provided CLOS functionality. The book
The Art of the Metaobject Protocol
''The Art of the Metaobject Protocol'' (AMOP) is a 1991 book by Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow (all three working for Xerox PARC) on the subject of metaobject protocol.
Overview
The book contains an explanation of what ...
describes the use and implementation of the CLOS MOP.
The various Common Lisp implementations have slightly different support for the Meta-Object Protocol. The ''Closer'' project aims to provide the missing features.
Influences from older Lisp-based object systems
Flavors (and its successor New Flavors) was the object system on the MIT
Lisp Machine
Lisp machines are general-purpose computers designed to efficiently run Lisp as their main software and programming language, usually via hardware support. They are an example of a high-level language computer architecture, and in a sense, the ...
. Large parts of the Lisp Machine operating systems and many applications for it use Flavors or New Flavors. Flavors introduced
multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
and
mixins
In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depen ...
, among other features. Flavors is mostly obsolete, though implementations for Common Lisp do exist. Flavors was using the message passing paradigm. New Flavors introduced generic functions.
CommonLoops CommonLoops (the Common Lisp Object-Oriented Programming System; an acronym reminiscent of the earlier Lisp OO system "Loops" for the Interlisp-D system) is an early programming language which extended Common Lisp to include Object-oriented progr ...
was the successor of LOOPS (from Xerox
Interlisp
Interlisp (also seen with a variety of capitalizations) is a programming environment built around a version of the programming language Lisp. Interlisp development began in 1966 at Bolt, Beranek and Newman (renamed BBN Technologies) in Cambridge, ...
-D). CommonLoops was implemented for Common Lisp. A portable implementation called Portable CommonLoops (PCL) was the first implementation of CLOS. PCL is widely ported and still provides the base for the CLOS implementation of several
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
implementations. PCL is implemented mostly in portable Common Lisp with only a few system dependent parts.
CLOS in other programming languages
Because of the power and expressivity of CLOS, as well as the historical availability of Tiny CLOS (a simplified portable CLOS implementation written by
Gregor Kiczales
Gregor Kiczales is an American computer scientist. He is currently a full time professor of computer science at the University of British Columbia in Vancouver, British Columbia, Canada. He is best known for developing the concept of aspect-orien ...
for use with Scheme), CLOS-like MOP-based object systems have become the ''de facto'' norm in most Lisp dialect implementations, as well as finding their way into some other languages'
OOP facilities:
* COS, the C Object System
*
Dylan
* Dynace, a (largely) CLOS implementation in C
EIEIOfor Emacs Lisp
Gauche Scheme with CLOS
in
GNU Guile
GNU Ubiquitous Intelligent Language for Extensions (GNU Guile) is the preferred extension language system for the GNU Project and features an implementation of the programming language Scheme. Its first version was released in 1993. In addition ...
* ILOS in
ISLISP
ISLISP (also capitalized as ISLisp) is a programming language in the Lisp family standardized by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC) joint working group ISO/IEC JTC 1/SC 22/W ...
Meroon an Object System in Scheme
a Scheme with CLOS
for Scheme
SOSfor MIT Scheme
STklos a Scheme with CLOS
Swindlein Racket
COOPSin Chicken Scheme
* VCLOS for
Skill
A skill is the learned ability to act with determined results with good execution often within a given amount of time, energy, or both. Skills can often be divided into domain-general and domain-specific skills. For example, in the domain of wo ...
* Tiny CLOS
Tiny CLOS, developed by Gregor Kiczales
/ref>
Further reading
*
*
References
Literature
* Sonya Keene, '' Object-Oriented Programming in Common Lisp: A Programmer's Guide to CLOS'', 1988, Addison-Wesley.
* Gregor Kiczales
Gregor Kiczales is an American computer scientist. He is currently a full time professor of computer science at the University of British Columbia in Vancouver, British Columbia, Canada. He is best known for developing the concept of aspect-orien ...
, Jim des Rivieres
Jim or JIM may refer to:
* Jim (given name), a given name
* Jim, a diminutive form of the given name James
* Jim, a short form of the given name Jimmy
* OPCW-UN Joint Investigative Mechanism
* ''Jim'' (comics), a series by Jim Woodring
* ''Ji ...
, and Daniel G. Bobrow
Daniel Gureasko Bobrow (29 November 1935 – 20 March 2017) was an American computer scientist who created an oft-cited artificial intelligence program STUDENT (computer program), STUDENT, with which he earned his PhD., worked at BBN Technologies ( ...
, ''The Art of the Metaobject Protocol
''The Art of the Metaobject Protocol'' (AMOP) is a 1991 book by Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow (all three working for Xerox PARC) on the subject of metaobject protocol.
Overview
The book contains an explanation of what ...
'', 1991, MIT Press.
* Jo A. Lawless and Molly M. Miller, '' Understanding CLOS: the Common Lisp Object System'', 1991, Digital Press,
* Andreas Paepcke
Andreas ( el, Ἀνδρέας) is a name usually given to males in Austria, Greece, Cyprus, Denmark, Armenia, Estonia, Ethiopia, Eritrea, Finland, Flanders, Germany, Norway, Sweden, Switzerland, Romania, the Netherlands, and Indonesia. The name ...
, '' Object-Oriented Programming: the CLOS Perspective'', 1993, The MIT Press.
The Common Lisp Object System: An Overview
by Richard P. Gabriel
Richard P. Gabriel (born 1949) is an American computer scientist known for his work in computing related to the programming language Lisp, and especially Common Lisp. His best known work was a 1990 essay "Lisp: Good News, Bad News, How to Win Bi ...
and Linda DeMichiel provides a good introduction to the motivation for defining classes by means of generic functions.
Fundamentals of CLOS
by Nick Levine provides a step-by-step exposure to the implementation of OO concepts in CLOS, and how to utilize them. It is intended for anybody with a basic knowledge of Lisp or Scheme.
* ''Common Lisp HyperSpec The Common Lisp HyperSpec is a technical standard document written in the hypertext format ''Hypertext Markup Language'' (HTML). It is not the American National Standards Institute
The American National Standards Institute (ANSI ) is a priv ...
''
Chapter 7: ''Objects''
{{Lisp programming language
Common Lisp
Object-oriented programming
Lisp (programming language)