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


You will notice a curious thing. This method for allocate-instance is used to allocate storage for instances of user-defined classes, but because standard-class is an instance of itself, it is also used to allocate storage for classes. In practical terms, this is not that weird; it just means that storgate for class objects is laid out exactly the same way as storage for ordinary instances. However, an inherent circularity occurs here: an instance cannot be made until its class exists, but the class of standard-class is itself. Luckily, only the implementors of CLOS need to worry about this, because it is really a problem only when a CLOS system is being bootstrapped -- kind of like picking yourself up by your belt and flying.

More interesting than allocate-instance is initialize-instance. When we discussed the initialization protocol, we saw what the purpose of the initialization protocol was and how initialize-instancefits into it. Listing 5 shows an after method for initialize-instance on standard-class that initializes the class object allocated by allocate-std-instance. It is an after method to follow the CLOS convention for methods on initialize-instance. Reading the code, we see it performs the following steps. First, it determines the superclass objects, which are either passed in or defaulted to standard-object. Next, it installs the newly defined class as a subclass of the superclasses. Then it takes each slot property list and turns it into a slot definition metaobject. The slot accessor methods are then defined, and finally inheritance-related actions are performed.

(defmethod initialize-instance :after
   ((class standard-class)
    key direct-superclasses direct-slots 
    allow-other-keys)
  (let ((supers
         (or direct-superclasses
             (list (find-class 'standard-object)))))
    (setf (slot-value class 'direct-superclasses) supers)
    (dolist (superclass supers)
      (push class (slot-value superclass 'direct-subclasses))))
  (let ((slots 
         (mapcar #'(lambda (slot-properties)
                     (apply #'make-direct-slot-definition
                            slot-properties))
                 direct-slots)))
    (setf (slot-value class 'direct-slots) slots)
    (dolist (direct-slot slots)
      (dolist (reader (slot-definition-readers direct-slot))
        (add-reader-method 
          class reader (slot-definition-name direct-slot)))
      (dolist (writer (slot-definition-writers direct-slot))
        (add-writer-method 
          class writer (slot-definition-name direct-slot)))))
  (finalize-inheritance class)
  (values))
Listing 5: An after method for intialize-instance.

The details of slot-definition meta-objects are not important. Let 's look at finalize-inheritance. In a real CLOS implementation, finalize-inheritance could not be called from within the initialization p rotocol because a class's superclasses might not be defined at that point. The definition of finalize-inheritance is:

(defmethod finalize-inheritance
    ((class standard-class)) 
    (setf (class-precedence-list class) 
           (compute-class-precedence-list class)) 
    (setf  (class-slots class) 
          (compute-slots class))
(values))

finalize-inheritance first computes the class precedence list and stores it in a slot in the class object. Next, it computes effective slot-definition metaobjects, which describe each slot whether direct or inherited. This computation considers the slot inheritancles of CLOS. Listing 6 shows the method for compute-slots, which makes a lis t of the effective slot definitions. Each effective slot definition is computed by gathering all the direct slot definitions for each slot name into a list in the most to least specific order (according to the class precedence list) and passing that list along with the class to compute-effective-slot-definition. compute-effective-slot-definition simply makes an object that represents a slot definition with en tries representing its name, initform (and initfunction), initargs, and allocation type. mapappend is like mapcar except that the results are appended together.

(defmethod compute-slots ((class standard-class)) 
  (let* ((all-slots (mapappend #'class-direct-slots
                               (class-precedence-list class)))
         (all-names (remove-duplicates 
                      (mapcar #'slot-definition-name all-slots))))
    (mapcar #'(lambda (name)
                (compute-effective-slot-definition
                  class
                  (remove name all-slots
                          :key #'slot-definition-name
                          :test-not #'eq)))
            all-names)))

(defmethod compute-effective-slot-definition
   ((class standard-class) direct-slots)
  (let ((initer (find-if-not #'null direct-slots
                             :key #'slot-definition-initfunction)))
    (make-effective-slot-definition
      :name (slot-definition-name (car direct-slots))
      :initform (if initer
                    (slot-definition-initform initer)
                    nil)
      :initfunction (if initer
                        (slot-definition-initfunction initer)
                        nil)
      :initargs (remove-duplicates 
                  (mapappend #'slot-definition-initargs
                             direct-slots))
      :allocation (slot-definition-allocation (car direct-slots)))))
Listing 6: Method for compute slots.

The important thing to see is that each slot has an entry in a list ; the relative position of an effective slot-definition entry in that list will determine where in the vector the slot is located. Note that we have not yet seen how slots are accessed. What we have seen is how the class object stores information that is used to access slots, and we've also seen how defclass eventually makes an instance of standard-class, which is a metaobject.


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.