On classes in declarations

Ursula Dreier Ursula.Dreier@ruhr-uni-bochum.de
Sat, 28 Nov 1998 23:22:09 +0100


My approach to the covariance/contravariance issue:

Not only class names (that is, literal constants) are allowable as a
type, but also expressions, as long as they yield a class at compile
time and give the editor a clue about the base class. Consider the
following:

function: CopyOmatic
 Parameters
  Number: x
 Local variables
  ClassOf(x): myCopy
 Returns: ClassOf(x):
 Code
  myCopy := x + 1
  return myCopy

How about that?
Inside the function you can only use items in the name space of Number,
e.g. the + operator, because x has been declared as a Number.
But the function will create and return an object of whatever a class x
is, as long as it is derived from Number.
And when compiling an invocation of this method, the compiler can
evaluate ClassOf (x) because the class of the expression being bound to
x is known at that place & time, and therefore regard the result of
CopyOmatic for this specific call to be of the type of that expression
rather than Number.

There need only be one copy of the compiled function CopyOmatic because
only items of name space Number will be referenced from within that
method (except for virtual functions, but that's done dynamically &
poses no problem), as opposed to the template function/class mechanism
of C++ where different incarnations of the code will be produced for
each different parameter set.

Of course we are not limited to the ClassOf method, we could as well use
BaseClassOf (x) or CommonAncestor (x, y) - you name it, as long as it
can be evaluated at compile time.

And of course this can be extended to cover the same topic for
(parameterized) classes.

This feature is a special case of constant folding (which means
evaluating constant expresssions at compile time and substituting the
resulting value for that expression). So it is nothing really
extraordinary.


Hans-Dieter Dreier