Slot description

When specifying a slot, a set of options can be given to the system. Each option is specified with a keywordkeyword. The list of authorised keywords is given below:

To illustrate slot description, we shall redefine the <complex> class seen before. A definition could be:
\begin{scheme}
(define-class <complex> (<number>)
((r :initform 0 :getter get-...
...r)
(i :initform 0 :getter get-i :setter set-i! :init-keyword :i)))
\end{scheme}
With this definition, the r and i slot are set to 0 by default. Value of a slot can also be specified by calling make with the :r and :i keywords. Furthermore, the generic functions get-r and set-r! (resp. get-i and set-i!) are automatically defined by the system to read and write the r (resp. i) slot.
\begin{scheme}
(define c1 (make <complex> :r 1 :i 2))
(get-r c1) \lev 1
(set-r! ...
...efine c2 (make <complex> :r 2))
(get-r c2) \lev 2
(get-i c2) \lev 0
\end{scheme}

Accessors provide an uniform access for reading and writing an object slot. Writing a slot is done with an extended form of set!set! which is close to the Common Lisp setf macro. So, another definition of the previous <complex> class, using the :accessor option, could be:
\begin{scheme}
(define-class <complex> (<number>)
((r :initform 0 :accessor re...
...keyword :r)
(i :initform 0 :accessor imag-part :init-keyword :i)))
\end{scheme}

Using this class definition, reading the real part of the c complex can be done with:
\begin{scheme}
(real-part c)
\end{scheme}
and setting it to the value contained in the new-value variable can be done using the extended form of set!.
\begin{scheme}
(set! (real-part c) new-value)
\end{scheme}

Suppose now that we have to manipulate complex numbers with rectangular coordinates as well as with polar coordinates. One solution could be to have a definition of complex numbers which uses one particular representation and some conversion functions to pass from one representation to the other. A better solution uses virtual slots. A complete definition of the <complex> class using virtual slots is given in Figure 2.

Figure: A <complex> number class definition using virtual slots
\begin{figure}{\footnotesize
\begin{quote}
\begin{quote}
\begin{verbatim}(defi...
...set! o 'i (* m (sin a))))))))\end{verbatim}
\end{quote}\end{quote}}
\end{figure}


This class definition implements two real slots (r and i). Values of the m and a virtual slots are calculated from real slot values. Reading a virtual slot leads to the application of the function defined in the :slot-ref:slot-ref option. Writing such a slot leads to the application of the function defined in the :slot-set!:slot-set! option. For instance, the following expression
\begin{scheme}
(slot-set! c 'a 3)
\end{scheme}
permits to set the angle of the c complex number. This expression conducts, in fact, to the evaluation of the following expression
\begin{scheme}
((lambda o m)
(let ((m (slot-ref o 'm)))
(slot-set! o 'r (* m (cos a)))
(slot-set! o 'i (* m (sin a))))
c 3)
\end{scheme}
A more complete example is given below:
\begin{scheme}
(define c (make <complex> :r 12 :i 20))
(real-part c) \lev 12
(an...
...ts are:
r = 1
i = 10
m = 10.0498756211209
a = 1.47112767430373
\end{scheme}

Since initialization keywords have been defined for the four slots, we can now define the make-rectangular and make-polar standard Scheme primitives.
\begin{scheme}
(define make-rectangular
(lambda (x y) (make <complex> :r x :i ...
... make-polar
(lambda (x y) (make <complex> :magn x :angle y)))
\par
\end{scheme}