Node:Class definition, Previous:Class definition and instantiation, Up:Class definition and instantiation
A new class is defined with the define-class
1 macro. The syntax of
define-class
is close to CLOS defclass
:
(define-class class (superclass ...) slot-description ... class-option ...)
Class options will not be discussed in this tutorial. The list of superclasses specifies which classes to inherit properties from class (see Inheritance for more details). A slot-description gives the name of a slot and, eventually, some "properties" of this slot (such as its initial value, the function which permit to access its value, ...). Slot descriptions will be discussed in Slot description.
As an example, let us define a type for representation of complex
numbers in terms of real numbers. This can be done with the following
class definition:
(define-class <complex> (<number>) r i)
This binds the variable <complex>
2 to a new class whose instances contain two
slots. These slots are called r
an i
and we suppose here
that they contain respectively the real part and the imaginary part of a
complex number. Note that this class inherits from <number>
which
is a pre-defined class. (<number>
is the direct super class of
the pre-defined class <complex>
which, in turn, is the super
class of <real>
which is the super of
<integer>
.)3.
Don't
forget to import the (oop goops)
module
<complex>
is in
fact a builtin class in GOOPS. Because of this, GOOPS will create a new
class. The old class will still serve as the type for Guile's native
complex numbers.
With the new definition of <complex>
,
a <real>
is not a <complex>
since <real>
inherits
from <number>
rather than <complex>
. In practice,
inheritance could be modified a posteriori, if needed. However,
this necessitates some knowledge of the meta object protocol and it will
not be shown in this document