- Common Lisp Object System
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:
defmethod. Instances are created with the function
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 are sharing 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.
Methods in CLOS are grouped into generic functions. 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.
; 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-valuefunction or via (optionally auto-generated) accessor methods. To access it via
slot-valueyou 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
: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
:aroundmethod can control whether the primary method is executed at all. Additionally, the programmer can specify whether all possible primary methods along the class hierarchy should be called or just the one providing the closest match.
The Standard Method-Combination provides above primary, before, after and around methods. 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. 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-classoperator. 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 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.
Outside of the ANSI Common Lisp standard, there is a widely implemented extension to CLOS called the Metaobject 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 metaclasses, and allows the definition of new metaclasses and the modification of all CLOS behavior. The flexibility of the CLOS MOP prefigures aspect-oriented programming, which was later developed by some of the same engineers, such as Gregor Kiczales. 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 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. Large parts of the Lisp Machine operating systems and many applications for it use Flavors or New Flavors. Flavors introduced multiple inheritance and mixins, amongst 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 was the successor of LOOPS (from Xerox Interlisp-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 implementations. PCL is implemented mostly in portable Common Lisp with only a few system dependent parts.
- ^ "CLOS is a standard. Multiple vendors supply CLOS. CLOS (or parts of it) is being used to add object-orientation to other Lisp dialects such as EuLisp or Emacs Lisp." pg 110 of Veitch 1998
- ^ In the Design Patterns in Dynamic Languages slides, Peter Norvig presents his findings that 16 out of 23 design patterns taken from various textbooks are either "invisible or simpler" in Dylan or Common Lisp than in C++.
- ^ Closer Project: Closer to MOP
- "CommonLoops: merging Lisp and object-oriented programming", by Daniel G. Bobrow, Kenneth Kahn, Gregor Kiczales, Larry Masinter, Mark Stefik, Frank Zdybel. 1986, Portland, Oregon, United States. Pages 17 - 29 of the Conference on Object-oriented Programming Systems Languages and Applications, ISSN 0362-1340.
- "A History and Description of CLOS", by Jim Veitch. Pages 107-158 of Handbook of Programming Languages, Volume IV: Functional and Logic Programming Languages, ed. Peter H. Salus. 1998 (1st edition), Macmillian Technical Publishing; ISBN 1-57870-011-6
- Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow, The Art of the Metaobject Protocol, 1991, MIT Press. ISBN 0-262-61074-4
- The Common Lisp Object System: An Overview by Richard P. Gabriel 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.
- Sonya Keene, Object-Oriented Programming in Common Lisp: A Programmer's Guide to CLOS, 1988, Addison-Wesley. ISBN 0-201-17589-4
- Common Lisp HyperSpec, Chapter 7: Objects
Implementations SoftwareLibrariesApplicationsDevelopment environments Publications Design committeeX3J13 · Guy Steele Other
Wikimedia Foundation. 2010.
См. также в других словарях:
Common lisp object system — Cette page est considérée comme un article à approfondir. Le Common Lisp Object System, souvent abrégé en CLOS (prononcé si lauss ), est l ensemble des primitives présentes dans le langage de programmation Common Lisp pour construire un programme … Wikipédia en Français
Common Lisp Object System — Das Common Lisp Object System (kurz: CLOS) ist die objektorientierte Erweiterung der Programmiersprache Common Lisp. Es hat seine Ursprünge in Loops und Flavors, und ist im ANSI Standard für Common Lisp spezifiziert. CLOS wird (optional) durch… … Deutsch Wikipedia
Common Lisp Object System — Cette page est considérée comme un article à approfondir. Le Common Lisp Object System, souvent abrégé en CLOS (prononcé si lauss ), est l ensemble des primitives présentes dans le langage de programmation Common Lisp pour construire un programme … Wikipédia en Français
Common Lisp Object System — … Википедия
Common Lisp Object System (disambiguation) — Common Lisp Object System may refer to: Common Lisp Object System Clos network This disambiguation page lists articles associated with the same title. If an internal link led you here, yo … Wikipedia
Common Lisp — Paradigm(s) Multi paradigm: procedural, functional, object oriented, meta, reflective, generic Appeared in 1984, 1994 for ANSI Common Lisp Developer ANSI X3J13 committee Typing discipline … Wikipedia
Common Lisp — est un langage fonctionnel impur de la famille Lisp. Sommaire 1 Introduction 2 Syntaxe 3 Types de données 3.1 Types scalaires … Wikipédia en Français
Object-Oriented Programming in Common Lisp: A Programmer's Guide to CLOS — (1988, Addison Wesley, ISBN 0 201 17589 4) is a book by Sonya Keene on the Common Lisp Object System. Published first in 1988, the book starts out with the elements of CLOS and develops through the concepts of data abstraction with classes and… … Wikipedia
Common Lisp the Language — is an influential book by Guy L. Steele about Common Lisp. Contents 1 History 1.1 Before standardization 1.2 During standardization 1.3 A … Wikipedia
Common-Lisp — Inoffizielles Lisp Logo Basisdaten Paradigmen: multiparadigmatisch: funktional, prozedural … Deutsch Wikipedia