July 2001
Templates
Introduction
- Static typechecking; simple syntactical or semantic errors can be found at compile time instead of hunted down during run time.
- Generality; one general code definition can be used over many types.
- Optimization; limiting the domain of the definition allows for possible optimizations to the generic code.
Many languages have the concept of a template. C++ has exactly that under the same name. They also be seen in Haskell, where they are called polymorphic types, and in Modula3, called generics. The use of templates has a three-fold benefit:
As we will show below, a template system can be constructed from a first order type system.
Construction of the Template_Class
A template is a Template Class, and inherits the Class Class' ability to generate class instances. There are two parts to defining a template. The first is indicating the parameters that the template will use, the other is defining the example. We can see the definition of the Template_Class here. Every template must have an example to exploit. The fields of any instance of the Template_Class will be used as both parameters for the example and be part of the example's structure.
CLASS Template_Class
EXTENDS Class_Class
FIELD Example AS Object_Class;
METHOD NewInstance() RETURNS Object_Class;
END Template_Class
The List_Class is an example of a Template_Class instance. It defines a parameter, T, to be used as the type of object the list will handle. The List_Template object is used as the example. List_Template defines the interface and function definition, using T instead of a specific class. Note that T is both an instance of Field_Class and an instance of Class_Class for use in the List_Template example.
CLASS List_Class
AS Template_Class EXTENDS Class_Class
FIELD T AS Class_Class;
Example=List_Template;
END List_Class
CLASS List_Template
METHOD findObject(Object AS T) RETURNS Boolean_Class;
METHOD getObjectByIndex(index AS Integer_Class) RETURNS T;
METHOD addObject(Object AS T) RETURNS NULL;
METHOD deleteObject(Object AS T) RETURNS NULL;
END List_Template
Now that the definition portion of the template is complete, we can define a useful List_Of_Integers_Class. The List_Class is an instance of the Template_Class, therefore it has the executable function NewInstance:
List_Of_Integers_Class = List_Class.NewInstance(T=Integer_Class);
This definition is exactly like the following, only much less work:
CLASS List_Of_Integers_Class
METHOD findObject(Object AS Integer_Class) RETURNS Boolean_Class;
METHOD getObjectByIndex(index AS Integer_Class) RETURNS Integer_Class;
METHOD addObject(Object AS Integer_Class) RETURNS NULL;
METHOD deleteObject(Object AS Integer_Class) RETURNS NULL;
END List_Of_Integers_Class
Templates are a Generalization of Parametric Polymorphism
A type is a domain definition, and is usually synonymous with the class definition. Some types can be defined such that they require parameters and is called parametric polymorphism. If these parameters happen to be types themselves, then the defining type is a second order type.
As seen on the List_Class example templating is useful in implementing parametric polymorphism. But templating need not limit itself to classes, and in this way generalized parametric polymorphism to general parametric instantiation. Templates have been first implemented in the DBOS graphics system. Any collection of graphic elements can be parameterized and wrapped in a template definition. In this way, any set of objects, that only differ by a set of constants, can be efficiently described. Efficient descriptions is the purpose behind implementing parametric polymorphism.
Templates are a Generalization of Meta Objects (Classes)
This is a very important realization. The ability of the template to turn an example into a class instance allows any object to be a prototype for many others. This is exactly what a class is. The only distinction between a prototype system and a class system is the extra type information attributed to the class. This extra type information is useful and the advantages to the programmer outweigh the niceties of a uniform template system. The DBOS philosophy dictates that classes and templates both exist for semantic brevity.
Other Notes
Versioning also takes a part in the definition of a template instance. It is important to realize that, even though there is an explicit instance, it can not be altered by the forces outside the template definition. Therefore the context manager for a template must know that all the attributes of the explicit template instance are immutable.
July 12, 2000, initial draft
July 23, 2001, correct and complete
.