Chapter 6. Object System

By Robert Strandh

RScheme is a fully object-oriented language in that each RScheme object is an instance of some class. Contrary to common languages such as C++ or Java in which methods are associated with classes, the RScheme object system is based on generic functions much in the spirit of Dylan, which in turn is based on the Common Lisp object system (CLOS).

Every value in RScheme is an object. That is, RScheme is "objects all the way down" rather than being a hybrid language like C++ or most Lisp object systems---the built-in R4RS Scheme data types are all classes in the class hierarchy, as they should be. Currently, only a simple object system with single inheritance and single dispatching is supported. The object system is approximately a subset of TinyCLOS, with some Dylan-like extensions.


Table of Contents
define-class -- Defines a new class

As mentioned above, every object in RScheme is an instance of a class. For example, 123 and 12.3 are both instances of the class named <number>, and "hello" and "hi there" are both instances of the class <string>.

Classes are organized into a hierarchy. The top class of the hierarchy is called <object>. If some class A is positioned immediately above some other class B in the hierarchy then we say that A is the immediate superclass of B, and B is the immediate subclass of A. All classes above A in the hierarchy are called (indirect) superclasses of B, and all classes below B in the hierarchy are called (indirect) subclasses of A. The term subclass is used to mean direct or indirect sublcass, and the term superclass is used to mean direct or indirect superclass. A direct subclass B is said to inherit from its direct superclass A, because instances of B automatically contain everything that instances of A contain, plus information specific to B. An instance of a class B is also said to be an instance of every superclass (direct or indirect) of B. An object, instance of B but not of any subclass of B, is said to be a direct instance of B and an (indirect) instance of every superclass of B.

In the RScheme class hierarchy, each class has exactly one direct superclass (except <object> which has none). Another term for this kind of class organization is single inheritance.

We mentioned above that 123 and 12.3 are both instances of the class <number>. To be more specific, 123 is a direct instance of the class named <fixnum> which is a subclass of <number>, and 12.3 is a direct instance of the class named <double-float> which is also a subclass of <number>.

In some languages, for instance C++ and Java, classes exist only at compile time, and thus cannot be manipulated as objects at runtime. In other languages, for instance Smalltalk and RScheme, classes are themselves objects (and thus instances of some classes) in the same way numbers and strings are objects. If classes can be manipulated in the same way other objects are manipulated, then we say that classes are first-class. If an object is first-class, it can be given as values to a variable, passed as an argument to a function, and returned as values of a function invocation.

Since classes are first-class objects, they do not necessarily have names. It is convenient, however, to be able to refer to classes by some name. Indeed, above we already talked about the classes named <object>, <number>, and <string>. In reality, these names are just names of ordinary global variables whose values happen to be classes. As a convention, we use angle brackets around variable names whose values are classes.

Most classes in RScheme are instances of the class named <<standard-class>>. Classes of class objects are called metaclasses. Names of metaclasses are surrounded by double angle brackets as a convention.

The most convenient way of creating a class and giving it a name, is to use the macro define-class. Here is the syntax: