ArticleslgStudy

science

Common Lisp Object System

Common Lisp Object System is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Common Lisp Object System rather than just read about it. In short: The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java.

Common Lisp Object System — main illustration
Common Lisp Object System — illustration

Key takeaways

  • Common Lisp Object System belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Common Lisp Object System to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Common Lisp Object System from memory before moving on to harder problems.

Reference excerpt

The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a 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 such as EuLisp or Emacs Lisp.

Features The basic building blocks of CLOS are methods, classes, instances of those classes, and generic functions. 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. 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. 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. For example,

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 methods. 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 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. 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 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 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.

… excerpt ends here. Continue reading the full article.

Illustrations

Common Lisp Object System: Standard method combination in ANSI Common Lisp
Standard method combination in ANSI Common Lisp

Worked examples

Example 1 — a first encounter with Common Lisp Object System

Start with the simplest possible case. Write down what Common Lisp Object System claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Common Lisp Object System before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Common Lisp Object System ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Common Lisp Object System

In research
Common Lisp Object System appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Common Lisp Object System in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Common Lisp Object System is common in secondary-school and first-year university syllabi. It links to neighbouring topics Common Lisp, Lisp (programming language), Object-oriented programming, so understanding it makes those chapters shorter.
In everyday life
Look for Common Lisp Object System outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Common Lisp Object System” →

How to study Common Lisp Object System in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Common Lisp Object System means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Common Lisp Object System out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Common Lisp Object System in simple terms?

The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java.

Why does Common Lisp Object System matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Common Lisp Object System?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Common Lisp Object System.

Tags

  • Common Lisp
  • Lisp (programming language)
  • Object-oriented programming

Keep exploring