8.3 Generic type specialization

Once a generic type is defined, it can be used to generate other types: this is like replaying the definition of the types, with the template placeholders filled in with actual type definitions.

This can be done in any Type definition block. The specialized type looks as follows:

Specialized type

--specialized type specialize -identifier- < type identifier list >----------

--type identifier list-|identifier---------------------------------------

Which is a very simple definition. Given the declaration of TList in the previous section, the following would be a valid type definition:

  TPointerList = specialize TList<Pointer>;  
  TIntegerList = specialize TList<Integer>;

As of version 3.0 of Free Pascal, the specialize keyword can also be used in a variable declaration:

  P : specialize TList<Pointer>;

The type in the specialize statement must be known, except in another generic type definition. Given the 2 generic class definitions:

  Generic TMyFirstType<T1> = Class(TMyObject);  
  Generic TMySecondType<T2> = Class(TMyOtherObject);

Then the following specialization is not valid:

  TMySpecialType = specialize TMySecondType<TMyFirstType>;

because the type TMyFirstType is a generic type, and thus not fully defined. The compiler will complain:

Error: Generics cannot be used as parameters when specializing generics

However, the following is allowed:

  TA = specialize TMyFirstType<Atype>;  
  TB = specialize TMySecondType<TA>;

because TA is already fully defined when TB is specialized.

However, the specialize keyword can be used in another generic type definition as shown in the example above:

  generic TList<_T>=class(TObject, specialize IList<_T>)


  generic TPointSet<t> = array of specialize PlanarCoordinate<t>;

In these definitions, the specialization is only performed when the generic type itself is specialized, and at that time, the types are known.

Remark: As of version 3.0, it is possible to make a forward definition of a generic class. In prior versions the compiler would generate an error if a forward declaration of a class was later defined as a generic specialization. This means that the following is now possible:

{$mode objfpc}  
  TMyClass = Class;  
  // Other declarations  
  TMyClass = specialize TList<T>;