[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 3. Slots

An instance has zero or more named slots; the name of a slot is a symbol. The slots of an instance are determined by its class.

Each slot can hold one value. When a slot does not have a value, the slot is said to be uninitialized. The default initial value for a slot is defined by the `initial-value` and `initializer` slot properties.

A slot is said to be accessible in an instance of a class if the slot is defined by the class of the instance or is inherited from a superclass of that class. At most one slot of a given name can be accessible in an instance. Slots are accessed by means of slot-access methods (usually generated by `make-class`).

> 3.1 Slot Descriptors |  |
> 3.2 Slot Access Methods |  |
> 3.3 Slot Access Constructors |  |
> 3.4 Slot Access Procedures |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.1 Slot Descriptors

Slots are represented by slot descriptors, which are data structures providing information about the slots, such as their name. Slot descriptors are stored inside of classes, and may be retrieved from there and subsequently inspected.

Procedure: class-slots class
  Returns a list of the slot descriptors for class. This contains all slots for class, both direct slots and inherited slots. The returned value must not be modified.

Procedure: class-slot class name error?
  Returns the slot descriptor for the slot named name in class. If there is no such slot: if error? is `#f`, returns `#f`, otherwise signals an error of type `condition-type:no-such-slot`.

Procedure: slot-descriptor? object
  Returns `#t` if object is a slot descriptor, otherwise returns `#f`.

Procedure: slot-name slot
  Returns the name of slot.

Procedure: slot-class slot
  Returns the class of slot. This is the class with which slot is associated. This is not necessarily the class that defines slot; it could also be a subclass of that class. If the slot was returned from `class-slots` or `class-slot`, then this class is the argument passed to that procedure.

Procedure: slot-properties slot
  Returns an alist of the properties of slot. This list must not be modified.

Procedure: slot-property slot name default
  If slot has a property named name, it is returned; otherwise default is returned.

Procedure: slot-initial-value? slot
  Returns `#t` if slot has an initial value, and `#f` otherwise. The initial value is specified by the `initial-value` slot property when a class is made.

Procedure: slot-initial-value slot
  Returns the initial value for slot, if it has one; otherwise it returns an unspecified value. The initial value is specified by the `initial-value` slot property when a class is made.

Procedure: slot-initializer slot
  Returns the initializer for slot; the initializer is specified by the `initializer` slot property when a class is made. This is a procedure of no arguments that is called to produce an initial value for slot. The result may also be `#f` meaning that the slot has no initializer.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.2 Slot Access Methods

The procedure `make-class` provides slot properties that generate methods to read and write slots. If an accessor is requested, a method is automatically generated for reading the value of the slot. If a modifier is requested, a method is automatically generated for storing a value into the slot. When an accessor or modifier is specified for a slot, the generic procedure to which the generated method belongs is directly specified. The procedure specified for the accessor takes one argument, the instance. The procedure specified for the modifier takes two arguments, the instance and the new value, in that order.

All of the procedures described here signal an error of type `condition-type:no-such-slot` if the given class or object does not have a slot of the given name.

Slot-access methods can be generated by the procedures `slot-accessor-method`, `slot-modifier-method`, and `slot-initpred-method`. These methods may be added to a generic procedure by passing them as arguments to `add-method`. The methods generated by these procedures are equivalent to those generated by the slot properties in `make-class`.

Procedure: slot-accessor-method class name
  Returns an accessor method for the slot name in class. The returned method has one required argument, an instance of class, and the specializer for that argument is class. When invoked, the method returns the contents of the slot specified by name in the instance; if the slot is uninitialized, an error of type `condition-type:uninitialized-slot` is signalled.

      (define-generic get-bar (object))

      (add-method get-bar
        (slot-accessor-method <foo> 'bar))

Procedure: slot-modifier-method class name
  Returns a modifier method for the slot name in class. The returned method has two required arguments, an instance of class and an object. The specializer for the first argument is class and the second argument is not specialized. When invoked, the method stores the second argument in the slot specified by name in the instance.

      (define-generic set-bar! (object bar))

      (add-method set-bar!
        (slot-modifier-method <foo> 'bar))

Procedure: slot-initpred-method class name
  Returns an "initialized?" predicate method for the slot name in class. The returned method has one required argument, an instance of class, and the specializer for that argument is class. When invoked, the method returns `#t` if the slot specified by name is initialized in the instance; otherwise it returns `#f`.

      (define-generic has-bar? (object))

      (add-method has-bar?
        (slot-initpred-method <foo> 'bar))

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.3 Slot Access Constructors

For convenience, and for consistency with the record-accessor procedures `record-accessor` and `record-modifier`, each of the above method-generating procedures has a corresponding accessor-generator. Each of these procedures creates a generic procedure, adds an appropriate method to it by calling the corresponding method-generating procedure, and returns the generic procedure. Thus, for example, the following are equivalent:

    (slot-accessor <foo> 'bar)

    (let ((g (make-generic-procedure 1)))
      (add-method g (slot-accessor-method <foo> 'bar))
      g)

Procedure: slot-accessor class name
  Returns a generic procedure of one argument that is an accessor for the slot name in class. The argument to the returned procedure must be an instance of class. When the procedure is called, it returns the contents of the slot name in that instance; if the slot is uninitialized, an error of type `condition-type:uninitialized-slot` is signalled.

Procedure: slot-modifier class name
  Returns a generic procedure of two arguments that is a modifier for the slot name in class. The first argument to the returned procedure must be an instance of class, and the second argument may be any object. When the procedure is called, it modifies the slot name in the instance to contain the second argument.

Procedure: slot-initpred class name
  Returns a generic procedure of one argument that is an "initialized?" predicate for the slot name in class. The argument to the returned procedure must be an instance of class. When the procedure is called, it returns `#t` if the slot name in that instance is initialized, otherwise it returns `#f`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.4 Slot Access Procedures

Finally, there is another set of three procedures, which access the contents of a slot directly, given an instance and a slot name. These procedures are very slow by comparison with the above.

However, note the following. You can use these procedures in the body of a `define-method` special form in an efficient way. If the `define-method` specifies the correct number of arguments, the body of the form contains a call to one of these procedures and nothing else, and the specified slot name is quoted, the form is rewritten during macro-expansion time as a call to the corresponding method-generating procedure. For example, the following are equivalent:

    (define-method p ((v <foo>))
      (slot-value v 'bar))

    (add-method p
                (slot-accessor-method <foo> 'bar))

Procedure: slot-value instance name
  Returns the contents of the slot name in instance; if the slot is uninitialized, an error of type `condition-type:uninitialized-slot` is signalled.

Procedure: set-slot-value! instance name object
  Modifies the slot name in instance to contain object.

Procedure: slot-initialized? instance name
  Returns `#t` if the slot name in instance is initialized, otherwise returns `#f`.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Chris Hanson on March, 7 2002 using texi2html