8.3 Generic class specialization

Once a generic class is dened, it can be used to generate other classes: this is like replaying the denition of the class, with the template placeholders lled in with actual type denitions.

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

_________________________________________________________________________________________________________Specialized type
-- --specialized type specialize -identi  er <  type identi  er list >----------

-- --type identi  er list-identi  er--------------------------------------
                    6---,-----|
___________________________________________________________________

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

Type  
  TPointerList = specialize TList;  
  TIntegerList = specialize TList;

The following is not allowed:

Var  
  P : specialize TList;

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 denitions:

type  
  Generic TMyFirstType = Class(TMyObject);  
  Generic TMySecondType = Class(TMyOtherObject);

Then the following specialization is not valid:

type  
  TMySpecialType = specialize TMySecondType;

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

type  
  TA = specialize TMyFirstType;  
  TB = specialize TMySecondType;

because TA is already fully dened 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;  
  TB = specialize TList;

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 denition of a generic class. The compiler will generate an error if a forward declaration of a class is later dened as a generic specialization.