A new class is defined with the define-class syntax:
(define-class class (superclass ...)
slot-description ...
class-option ...)
class is the class being defined. The list of superclasses specifies which existing classes, if any, to inherit slots and properties from. Slots hold per-instance1 data, for instances of that class — like “fields” or “member variables” in other object oriented systems. Each slot-description gives the name of a slot and optionally some “properties” of this slot; for example its initial value, the name of a function which will access its value, and so on. Class options, slot descriptions and inheritance are discussed more below.
Define a class called name that inherits from supers, with direct slots defined by slot-definitions and class options options. The newly created class is bound to the variable name name in the current environment.
Each slot-definition is either a symbol that names the slot or a list,
(slot-name-symbol . slot-options)where slot-name-symbol is a symbol and slot-options is a list with an even number of elements. The even-numbered elements of slot-options (counting from zero) are slot option keywords; the odd-numbered elements are the corresponding values for those keywords.
options is a similarly structured list containing class option keywords and corresponding values.
As an example, let us define a type for representing a complex number in terms of two real numbers.2 This can be done with the following class definition:
(define-class <my-complex> (<number>)
r i)
This binds the variable <my-complex> to a new class whose
instances will contain two slots. These slots are called r and
i and will hold the real and imaginary parts of a complex
number. Note that this class inherits from <number>, which is a
predefined class.3
Slot options are described in the next section. The possible class options are as follows.
The
#:metaclassclass option specifies the metaclass of the class being defined. metaclass must be a class that inherits from<class>. For the use of metaclasses, see Metaobjects and the Metaobject Protocol and Metaclasses.If the
#:metaclassoption is absent, GOOPS reuses or constructs a metaclass for the new class by callingensure-metaclass(see ensure-metaclass).
The
#:nameclass option specifies the new class's name. This name is used to identify the class whenever related objects - the class itself, its instances and its subclasses - are printed.If the
#:nameoption is absent, GOOPS uses the first argument todefine-classas the class name.
[1] Usually — but
see also the #:allocation slot option.
[2] Of course Guile already
provides complex numbers, and <complex> is in fact a predefined
class in GOOPS; but the definition here is still useful as an
example.
[3] <number> is the direct superclass of
the predefined class <complex>; <complex> is the
superclass of <real>, and <real> is the superclass of
<integer>.