Contents | Prev | Next | Index


Class Reference Types

Each class has exactly one runtime descriptor containing information about the class type. This descriptor contains information about the class. It also has methods for operating on classes. This descriptor is called a class reference type in Pascal. It is also more commonly known as a metaclass.
class_ref_type   ::= CLASS OF qualident

The class reference type is useful for creating objects whose actual types are unknown at compile time. Example:

USES java_lang_Object, java_lang_Class;
  :
TYPE
  TFigure = CLASS( Object ) ... END;
  TFigureClass = CLASS OF TFigure;
  TRectangle = CLASS( TFigure ) ... END;
  TSquare = CLASS( TRectangle ) ... END;
  TCircle = CLASS( TFigure ) ... END;
    :
FUNCTION CreateFigure
( metaClass : TFigureClass ) : TFigure;
BEGIN
  { depending on the dynamic type of FigureClass,
    this function might return a new instance of
    TFigure, TRectangle, TSquare or TCircle.
  }
  Result := metaClass.newInstance;
END;

   

Notice, that Borland Object Pascal implements class reference types in a different way, with metaclasses based on SYSTEM.TClass. Canterbury Pascal metaclasses are always mapped to java.lang.Class. Borland Object Pascal implements class methods as members of  its metaclass, while Canterbury Pascal always maps them to Java-static members of the class itself.

Borland Object Pascal uses metaclasses with virtual constructors to create objects  whose actual types are unknown at compile time. Canterbury Pascal achieves the same using the method java.lang.Class.newInstance, as shown in the above example. Or Canterbury Pascal can also use the method java.lang.Class.getConstructor for finding the correct pointer to a constructor method (of type java.lang.reflect.Constructor) which in turn can be invoked with a non-empty parameter list.

Unlike Borland Object Pascal, Canterbury Pascal in its current compiler version does not support other usages of the class reference type. However, the class operators IS and AS are supported for right side static type operands. Hence it is possible for performing type checks for objects whose type is unknown at compile time.


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com