From Lazarus wiki
Revision as of 10:47, 31 October 2010 by Chronos (talk | contribs)
Jump to navigationJump to search


Templates is simple mechanism to solve problem of writing duplicate code to implement general class for specific type in strong typed language. It is used mainly for base non-object types as in case of objects inheritance of single base class TObject can be used to build general classes. In advanced languages templates are replaced by native language implementation called generics. Native implementation of generics in FPC 2.4.0 is not complete neither practically usable yet. Then templates could be used temporary in the meantime.


  • Type safe - no need to do annoying typecasting. Less chance to do error in typecasting.
  • Better maintainability - Generic class code is written only once and used for all specialized classes
  • Better code reuse - It is possible to create good generic reusable class set
  • Better efficiency, scalability - generic classes types can be better adjusted for particular needs. On 16-bit machine specialized TList with 16-bit SmallInt index type could be used.


  • More complex
  • Bigger code - Every specialized class is compiled separately and produce additional code


Classical example of basic generic class is list of items. Current Free Pascal offer three ways of use lists:

  • array of - classical dynamic array structure which can hold dynamic items of same type. Items are accessible by direct addressing SomeArray[Index]. SetLength and Length functions are used to handle size.
  • TList - objective way where class handle all list operations. Implementation in LCL keep compatibility with VCL where TList holds list of Pointers. If list of different item type is desired complete new class have to be copied and rewritten or typecasting have to be used in every place where list item is referenced. Typecasting is not effective e.g. Pointer(Byte) and not type safe Pointer(Int64). TList index is of type Integer. If Int64 or SmallInt index is needed than class have to be copied and rewritten.
  • TCollection - is more generic and type safe but heavier solution for storing list of items. In this approach programmer have to create item class which inherits from TCollectionItem and set this newly created class type to TCollection constructor parameter.
  • native generics - not complete implementation yet. Lack support of generic type reference, inheritance, constraints.
  • templates - use code inclusion to workaround lack of native support. Can use inheritance and generic type reference. Constraints are given by used operators and other functions in generic class.

Templates tries to solve problem using concept of parametrized templates. Generic unit is written only once as template with use of generic not specified types in both interface and implementation section. Than in process of specialization class template is used and general template parameters are replaced by specific types. In result new specialized class is created.

GenericListInterface.tpl: <delphi> // TGList<T> = class

 TGList = class
   Items: array of T;
   procedure Add(Item: T);

GenericListImplementation.tpl: <delphi>procedure TGList.Add(Item: T); begin

 SetLength(Items, Length(Items) + 1);  
 Items[Length(Items) - 1] := Item;


In process of specialization early created template files are included to new unit.

ListInteger.pas: <delphi>unit ListInteger;





 T = Integer; // T is specified to some exact type

{$INCLUDE 'GenericListInterface.tpl'}


 // TListInteger<Integer> = class
 TListInteger = class(TGList)
   // Additional fields and methods can be added here


{$INCLUDE 'GenericListImplementation.tpl'}


Finally we have new specialized unit called ListInteger and we can use our new specialized type in some code.

<delphi>program GenericTest; uses



 List: TListInteger;


   List := TListInteger.Create;



Generic classes could inherit from each other in way of non-generic classes do. Implementation is little difficult as only one specialized type of particular class can be defined per unit. To be able to specify generic type to generic type o other class some naming scheme should be kept.

Assume that we want to create advanced generic list TAdvancedList which have in addition Capacity field.

Base generic class definition: GenericListInterface.tpl: <delphi> // TGList<TListIndex, TListItem> = class

 TGList = class
   Items: array[TListIndex] of TListItem;
   procedure Add(Item: TListItem);

GenericListImplementation.tpl: <delphi>procedure TGList.Add(Item: TListItem); begin

 SetLength(Items, Length(Items) + 1);  
 Items[Length(Items) - 1] := Item;


Definition for enhanced generic class: GenericAdvancedListInterface.tpl: <delphi>

 TListIndex = TAdvancedListIndex;
 TListItem = TAdvancedListItem;

{$INCLUDE 'GenericListInterface.tpl'}

 // TGAdvancedList<TAdvancedListIndex, TAdvancedListItem> = class(TGList)
 TGAdvancedList = class
   Capacity: TAdvancedListIndex;

GenericAdvancedListImplementation.tpl: <delphi>{$INCLUDE 'GenericListImplementation.tpl'}</delphi>

No some specialization example: AdvancedListInteger.pas: <delphi>unit AdvancedListInteger;





 TAdvancedListIndex = Integer; // specified to some exact type
 TAdvancedListItem = Integer; // specified to some exact type

{$INCLUDE 'GenericAdvancedListInterface.tpl'}


 // TAdvancedListInteger<Integer, Integer> = class
 TAdvancedListInteger = class(TGAdvancedList)
   // Additional fields and methods can be added here


{$INCLUDE 'GenericAdvancedListImplementation.tpl'}


Now we have specialized class from generic class which inherits form other generic class.


It is good to have class which e.g. works as containers for any other type. Container class doesn't need to know anything about contained class. But some classes could benefits from knowledge of what can be done with contained class. In native generics implementation there is possibility to restrict group of types which can be used as type parameters of generic class. In templates there aren't such feature but constraints are done simply by usage of generic type in generic class. If generic type is used for addition and subtraction than only types which supports that operations can be used. String can be concatenated using plus operator too. But if multiplication is used than only numeric types are allowed. If generic type is used as array index, than only ordinal types can be used. If some e.g. Free method is used over generic type than constraint include all classes. So in templates operations performed with generic type limits set of usable types for specialization.

Theoretical constraint groups:

  • ordinal types
  • float types
  • records
  • arrays
  • sets
  • interface
  • procedure, function, constructor
  • classes with all descendants

Generic classes

There are many classes which can benefit form being generic:

  • TList - list of items, Add, Delete, Insert, Move, Exchange, Clear, Sort,
  • TDictionary - list of pairs key and value,
  • TStack - LIFO structure, Push and Pop
  • TQueue - FIFO structure, Enqueue and Dequeue
  • TRange or TInterval - two values structure, Distance
  • TSet - set of items, Add, Remove, Intersection, Complement, Union, Product
  • TStream - divided to TInputStream and TOutputStream
  • TTree - hiearchical structure with nodes
  • TMatrix - multidimensional list
  • TGraph
  • TPoint - could be 1D, 2D, 3D or multidimensional
  • TVector
  • TComplexNumber

There are some interesting specialized types which can replace non-generic types:

  • TList<Char> is similar to string
  • TList<Byte> is similar to simple memory area
  • TList<string> is similar to TStringList
  • TList<Pointer> is similar to classical TList
  • TList<TObject> is similar to TObjectList
  • TQueue<Byte> is similar to one direction byte stream
  • TDictionary<string,string> is cleaner implementation of TStringList Key-Value functionality

Existed generic libraries

Every programmer can build own library of generic classes.

Some ready to use classes:

  • TemplateGenerics - Lazarus package of some experimental generic classes. In addition to parametrized list item type it offer parametrized index type too. Can be downloaded from using svn client.
svn co http://svn.zdechov.net/svn/PascalClassLibrary/Generics/TemplateGenerics/ TemplateGenerics

See also

External links