Next: , Previous: , Up: GOOPS   [Contents][Index]


8.2 Class Definition

A new class is defined with the define-class syntax:

(define-class class (superclass …)
   slot-descriptionclass-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-instance29 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.

syntax: define-class name (super …) slot-definition … class-option …

Define a class called name that inherits from supers, with direct slots defined by slot-definitions and class-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.

Each class-option is an option keyword and corresponding value.

As an example, let us define a type for representing a complex number in terms of two real numbers.30 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.31

Slot options are described in the next section. The possible class options are as follows.

class option: #:metaclass metaclass

The #:metaclass class 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 #:metaclass option is absent, GOOPS reuses or constructs a metaclass for the new class by calling ensure-metaclass (see ensure-metaclass).

class option: #:name name

The #:name class 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 #:name option is absent, GOOPS uses the first argument to define-class as the class name.


Footnotes

(29)

Usually — but see also the #:allocation slot option.

(30)

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.

(31)

<number> is the direct superclass of the predefined class <complex>; <complex> is the superclass of <real>, and <real> is the superclass of <integer>.


Next: , Previous: , Up: GOOPS   [Contents][Index]