[plt-scheme] Question about classes (class.ss)

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Jun 20 08:47:52 EDT 2002

Quoting Erich.Rast at t-online.de:
> My template system should allow the super class to inherit all of its 
> super class methods (incl. property getters and setters) to the new 
> 'external' class, provided the child class does not declare them. 
> However, there is no way for a new 'external' class to know all 
> superclass methods in advance.
> Is there an official way for the child class to automatically inherit 
> _all_ superclass methods _except_ the new methods defined in the 
> child class?
> In other words: With declaration (inherit-all), a child class should 
> automatically override any superclass method with the same name, but 
> otherwise inherit the superclass method.
> Is that possible with MzScheme's class.ss library?

By "inherit", you mean "bring into the scope of the class body"? A
class in MzScheme always inherits or overrides every method of its
superclass, but the names of the inherited methods are not
automatically brought into scope in the subclass's body.

The reason is the classes are first-class values, and the superclass
position is an expression position (with no type information).
Consequently, the methods of the superclass are not stctically known.

More concretely, suppose that `inherit-all' existed and I write

  (define (mk-class super)
    (class super

What names become bound in "..."? There's clearly no good answer,
because I might supply any class at all as the `super' argument.

So, no, it can't be done with MzScheme's "class.ss" library.

If I really needed something like this, I'd probably define syntax like

  (define-class-shape cowboy


   (class/shape (super cowboy)
      ... ; draw and shoot are bound to methods

In this case, the first-order "shape" declaration carries the
information that the class body needs. FWIW, this is similar to they
way that we make `match' work on imported structure types within a unit
(where the shape of an imported structure type is specified in the
import signature).


Posted on the users mailing list.