From Lazarus wiki
Revision as of 10:48, 29 October 2010 by Chronos (talk | contribs) (pros and cons)


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 = 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;




Existed generic libraries

Every programmer can build own library of generic classes. There are 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