On this page:
7.1 Interfaces
7.2 Mixins

7 A World of Possibilities

Éric Tanter

In this brief step-by-step construction of object systems in Scheme, we have only illustrated some fundamental concepts of object-oriented programming languages. As always in language design, there is a world of possibilities to explore, variations on the same ideas, or extensions of these ideas.

Here is just a (limited/arbitrary) list of features and mechanisms that you will find in some existing object-oriented programming languages, which were not covered in our tour, and that you may want to try to integrate in the object system. It is—of course—very interesting to think of other features by yourself, as well as to study existing languages and figure out how to integrate their distinctive features.

There are also many optimizations, such as:
  • compute fields offset for direct field access

  • vtables & indices for direct method invocation

We now only briefly outline two mechanisms, interfaces and mixins, as well as their combination (ie. using interfaces in the specification of mixins).

7.1 Interfaces

Introduce a form to define interfaces (which can extend super interfaces):

(interface (superinterface-expr ...) id ...)

Introduce a new class form that expects a list of interfaces:

(CLASS* super-expr (interface-expr ...) decls ...)

Example:
(define positionable-interface
  (interface () get-pos set-pos move-by))
 
(define Figure
  (CLASS* Root (positionable-interface)
     ....))
Extend the protocol of a class so as to be able to check if it implements a given interface:
> (implements? Figure positionable-interface)
#t

7.2 Mixins

A mixin is a class declaration parameterized over its superclass. Mixins can be combined to create new classes whose implementation sharing does not fit into a single-inheritance hierarchy.

Mixins "come for free" by the mere fact of having classes be first-class values integrated with functions.
(define (foo-mixin cl)
  (CLASS cl (....) (....)))
 
(define (bar-mixin cl)
  (CLASS cl (....) (....)))
 
(define Point (CLASS () ....))
 
(define foobarPoint
  (foo-mixin (bar-mixin Point)))
(define fbp (foobarPoint 'create))
....

Combine with interfaces to check that the given base class implements a certain set of interfaces. Define a MIXIN form for that:

(MIXIN (interface-expr ...) decl ...)

Defines a function that takes a base class, checks that it implements all the specified interfaces, and return a new class that extends the base class with the given declarations.