This can be done in any Type definition block. The specialized type looks as follows:
_________________________________________________________________________________________________________
Specialized type
___________________________________________________________________
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; |