manpagez: man pages & more
info guile
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.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-instance(24) 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 … . options

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.(25) 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.(26)

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 section 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.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on February 3, 2012 using texi2html 5.0.