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:
Which is a very simple definition. Given the declaration of TList in the previous section, the following would be a valid type definition:
As of version 3.0 of Free Pascal, the specialize keyword can also be used in a variable declaration:
The type in the specialize statement must be known, except in another generic type definition. Given the two generic class definitions:
Then the following specialization is not valid:
because the type TMyFirstType is a generic type, and thus not fully defined. The compiler will complain:
However, the following is allowed:
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:
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: