Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Parallel

Lisp: Classes in the Metaobject Protocol


The first thing to realize about the Common Lisp Object System (and LISP) is that many things that are very much behind the scenes in other languages are availab le as user-accessible data objects in CLOS. For example, in C++, only the compiler has any data structures that correspond to user-defined classes. In CLOS, user-defined classes are represented by objects -- class objects -- that can be examined and manipulated by user programs. This property is sometimes called "introspection."

When you create a class you get a class object back:

(defclass rectangle ()
((height :initform 0.0 
             :initarg :height) 
 (width :initform 0.0 
             :initarg :width)))
 #<Standard-Class F00>

The exact contents of the class object depend on the implementation of CLOS.

CLOS was designed in layers. The outermost layer (the one that programmers will typically use) contains the syntax of CLOS, defined by macros and functions such as defclass and defmethod. This layer is called the "user interface layer", but it isn't a user interface in the keyboard, mouse, window, graphics sense; it's a use interface to the operational part of the CLOS language. This user interface corresponds to language definition in other programming languages.

The next lower layer is the connection layer, and it is implemented by functions. The user interface macros expand to calls to function s in this layer; in addition, this layer contains functions that map names to metaobjects. For example, find-class is a function in this layer. The deepest layer is the metaobject layer. Metaobjects are first-class LISP objects that provide the behavior of classes, instances, slot access, generic functions, inheritance, and method combination.

One way to understand this layering is by analogy to cars. You can think of cars as having three layers just like CLOS. The outermost layer is almost like a user interface. This layer is basically all the controls (gas pedal, steering wheel, brakes). The driver interact s with these controls to make his car do things.

The next layer connects the controls to the things controlled. The gas pedal is connected by rods, levers, and springs to the carburetor. The function of this layer is to connect a device designed for easy human use to a device designed to accomplish its function readily. This is like the connection layer.

The deepest layer is the set of devices that make a car perform its function. For example, the carburetor controls the speed of the car by controlling the amount and rate of fuel burned. This is like the metaobject layer.

Changing the way a driver interacts with the car can simply involve the user interface layer; for example, if you want to change the sha pe of the gas pedal or slightly change its placement. For a larger change (such as implementing the gas pedal as a roller on the steering wheel), the interface and connection layer must be changed; that is, a new linkage must be designed and built as well as changing the gas pedal itself.

Finally, the nature of the car itself can be changed by changing the lowest layer. The engine can be replaced with a much more powerful one for amateur racing or trailer pulling. The suspension can be altered to make a road racing car. The back seats and rear sections can be altered to form a pickup truck. The body and undermachinery can be enclosed in a waterproof shell and a propeller added to create an amphibious vehicle. Some of these alterations require changing or a dding things to the connection and user interface layers, but the fundamental nature of the car cannot be changed without changing the deepest layer.

Some changes are just not possible unless the car is thrown away and a new design produced. An Indy car cannot be made from a stock model, no matter how much customization you do. Neither can you create an airplane from a car.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.