Scheme Programming/Object Orientation

There are a number of object systems for Scheme. This chapter will take a look at different ones.

Virgo
One library for objects in R7RS is Virgo. To import it, enter this: Many implementations, such as Chibi, Gauche, Guile, and Chicken, have their own CLOS-like systems, which may be substantially different in syntax but are very close to each other semantically. Virgo was chosen for portability, but the concepts will apply across implementations.

Defining a Class
This is the format for defining a class:

Constructing an Object
Simply use make to create a new object, with its values initialized.

Getting and Setting
The procedures for getting and setting values are slot-ref and slot-set!, respectively.

Generics
Unlike Java or C#, Virgo has a CLOS-like object system meaning that methods do not belong to a single class. Rather, we define a generic and then assign methods to that generic for handing different classes. Here is an example:

Methods can also work with multiple classes. To give a nonsense example:

Inheritance
Virgo also features inheritance. Here is an example:

Prometheus
Unlike CLOS-like systems, Prometheus uses prototype-based objects instead of classes. Also, methods are tied to objects similarly to Java or C#. Furthermore, objects are not disjoint types, but rather are procedures that interpret the first argument passed to it as a method name.

If you are using an R7RS implementation and have the Prometheus library installed, you can load the library with this:

Defining an Object
Defining objects is not very different from CLOS-like systems. Note that an object must inherit from another object unless it is a root object. If you do not want it to inherit from anything else, the object should inherit from *the-root-object*. Keeping our point example:

Cloning an Object
The define-object syntax is simply syntactic sugar, and you do not always need to use define-object to create a new instance. Instead you can clone an object like this, recalling that objects are just procedures:

Getting and Setting
Again, getting and setting are just passing different symbols to the object.

Methods
Of course, objects have methods associated with them. A method is a closure with at least two arguments: self, the object being passed to the closure, and resend, which calls the behavior of a parent object. The syntactic sugar for this is define-method:

Methods can also be defined for an object when it is defined with the define-object syntactic sugar.

YASOS
"YASOS", or "Yet Another Scheme Object System", is a particularly simple object system for Scheme. YASOS is very similar to the object system of T, an old dialect of Scheme. Let's look at its features.

If you are using an R7RS implementation and have the YASOS library installed, you can load the library with this: If you have SLIB installed and loaded, you can also do this:

Predicates and Operations
YASOS, compared to CLOS-like systems, may feel a bit inside-out. First, we declare operations and predicates, then we create objects. Let us keep with our point example for comparison:

Objects
Now that we have defined our operations for points, we will define an object that handles these operations in its methods. The syntax for objects' methods is similar to Prometheus. Rather than a built-in constructor syntax, we will just define a procedure that returns a newly constructed object. This design also means methods must be defined while constructing the object, and cannot be added after the fact.

Inheritance
YASOS uses the syntax object-with-ancestors to allow for inheritance, which will give the object characteristics of the "ancestor" or "parent" objects.