1.1 Class Datatype

The procedures in this section may be used to construct and inspect classes.

Procedure: make-class name direct-superclasses direct-slots

Creates and returns a new class object.

Name is used for debugging: it is a symbol that appears in the printed representation of the class and has no role in the semantics of the class. Alternatively, name may be #f to indicate that the class is anonymous.

Direct-superclasses must be a list of class objects. The new class inherits both methods and slots from the classes in this list. Specifying the empty list for direct-superclasses is equivalent to specifying (list <instance>).

Direct-slots describes additional slots that instances of this class will have. It is a list, each element of which must have one of the following forms:

name
(name . plist)

where name is a symbol, and plist is a property list. The first of these two forms is equivalent to the second with an empty plist.

Each of the elements of direct-slots defines one slot named name. Plist is used to describe additional properties of that slot. The following properties are recognized:

initial-value

This property specifies the default initial value for the slot, i.e. the value stored in the slot when an instance is created and no value is explicitly specified by the instance constructor. If neither the initial-value nor the initializer property is specified, the slot has no default initial value.

initializer

This property specifies a procedure of no arguments that is called by an instance constructor whenever an instance containing this slot is created. The value returned by the initializer procedure is the initial value of the slot.

accessor

This property specifies a generic procedure; make-class will add an accessor method for this slot to the procedure. See Slots.

modifier

This property specifies a generic procedure; make-class will add a modifier method for this slot to the procedure. See Slots.

initpred

This property specifies a generic procedure; make-class will add an “initialized?” predicate method for this slot to the procedure. See Slots.

Slot properties are combined in slightly complicated ways.

  • It is not allowed to specify both initial-value and initializer for a slot in a given call to make-class; at most one of these properties may be given.
  • If a slot is specified for a given class, and a slot of the same name is inherited from a superclass, then the slot properties for the two slots are combined. Slot properties from the subclass shadow those of the superclass. However, if a superclass has a slot property, and the subclass does not, the property is inherited. The resulting class never has more than one slot of a given name.
  • When combining superclass and subclass slots, initial-value and initializer shadow one another. In other words, regardless of the inherited slot properties, the resulting slot has at most one of these two properties.

Examples of make-class:

(define <cell>
  (make-class '<cell> '() '()))

(define-generic cell-name (cell))
(define-generic cell-width (cell))
(define-generic cell-height (cell))
(define-generic cell-components (cell))
(define-generic set-cell-components! (cell components))

(define <contact>
  (make-class '<contact>
              (list <cell>)
              `((name accessor ,cell-name))))

(define <compound-cell>
  (make-class '<compound-cell>
              (list <cell>)
              `((width accessor ,cell-width)
                (height accessor ,cell-height)
                (components accessor ,cell-components
                            modifier ,set-cell-components!
                            initial-value ()))))
Syntax: define-class name direct-superclasses direct-slot …

Define name to be a class. In its basic form, define-class might have been defined by

(define-syntax define-class
  (syntax-rules ()
    ((define-class name (class ...) slot ...)
     (define name
       (make-class (quote name)
                   (list class ...)
                   (quote (slot ...)))))))

Note that slot properties are handled specially by define-class. If a direct-slot specifies a slot properties property list, the keys of the property list (i.e. the even-numbered elements) are not evaluated, while the datums