Once a generic class is de ned, it can be used to generate other classes: this is like replaying the de nition of the class, with the template placeholders lled in with actual type de nitions.
This can be done in any Type de nition block. The specialized type looks as follows:
_________________________________________________________________________________________________________Specialized type
___________________________________________________________________
Which is a very simple de nition. Given the declaration of TList in the previous section, the following would be a valid type de nition:
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 de nitions:
type
Generic TMyFirstType Generic TMySecondType |
Then the following specialization is not valid:
type
TMySpecialType = specialize TMySecondType |
because the type TMyFirstType is a generic type, and thus not fully de ned. However, the following is allowed:
type
TA = specialize TMyFirstType TB = specialize TMySecondType |
because TA is already fully de ned 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 de nition of a generic class. The compiler will generate an error if a forward declaration of a class is later de ned as a generic specialization.