[Next] [Previous] [Up] [Top] [Contents] [Index]

Classes and instances

Allocating the value of a slot in a class

You can specifically instruct a slot to be associated not with an instance, but with the instance's class, by using the :allocation option when you define the slot.

The two :allocation options are :instance and :class. The :instance option is the default; this option means that each instance of a class has local storage for the slot that can be associated with its own value. The :class option specifies that the class stores the value of the slot. A slot of this kind is called a shared slot or a class slot. The following code defines picky-fourth-grader, associating it with a class slot for subjects-studied:

? (defclass picky-fourth-grader 
    (fourth-grader)
    ((subjects-studied 
      :initarg subjects-studied 
      :initform '(English math woodworking)
      :allocation :class)))
#<STANDARD-CLASS PICKY-FOURTH-GRADER>

Since all instances of picky-fourth-grader access the same subjects-studied slot, you can setf the class slot value through any instance:

? (setf zane (make-instance 'picky-fourth-grader))
#<PICKY-FOURTH-GRADER #x40CDC9>
? (setf justus (make-instance 'picky-fourth-grader))
#<PICKY-FOURTH-GRADER #x40EFC8>
? (setf (slot-value zane 'subjects-studied) 
   '(English math woodworking phenomenology))
(ENGLISH MATH WOODWORKING PHENOMENOLOGY)
? (slot-value justus 'subjects-studied)
(ENGLISH MATH WOODWORKING PHENOMENOLOGY)


Gettmg Started with MCL - 19 OCT 1996
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker