8.3 Generic class specialization

Once a generic class is defined, it can be used to generate other classes: this is like replaying the definition of the class, 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:

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

The following is not allowed:

Var  
  P : specialize TList<Pointer>;

that is, a variable cannot be directly declared using a specialization.

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

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

Then the following specialization is not valid:

type  
  TMySpecialType = specialize TMySecondType<TMyFirstType>;

because the type TMyFirstType is a generic type, and thus not fully defined. However, the following is allowed:

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

because TA is already fully defined when TB is specialized.

Note that 2 specializations of a generic type with the same types in a placeholder are not assignment compatible. In the following example:

type  
  TA = specialize TList<Pointer>;  
  TB = specialize TList<Pointer>;

variables of types TA and TB cannot be assigned to each other, i.e the following assignment will be invalid:

Var  
  A : TA;  
  B : TB;  
 
begin  
  A:=B;

Remark: It is not possible to make a forward definition of a generic class. The compiler will generate an error if a forward declaration of a class is later defined as a generic specialization.