Slot Access Methods - SOS 1.8
Description: SOS 1.8

Next: Slot Access Constructors, Previous: Slot Descriptors, Up: Slots

### 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))