Generics are templates for generating classes. It is a concept that comes from C++, where it is
deeply integrated in the language. As of version 2.2, Free Pascal also officially has support for
templates or Generics. They are implemented as a kind of macro which is stored in the unit
files that the compiler generates, and which is replayed as soon as a generic class is
specialized.
Currently, only generic classes can be defined. Later, support for generic records, functions and
arrays may be introduced.
Creating and using generics is a 2-phase process.
- The definition of the generic class is defined as a new type: this is a code template, a
macro which can be replayed by the compiler at a later stage.
- A generic class is specialized: this defines a second class, which is a specific
implementation of the generic class: the compiler replays the macro which was stored
when the generic class was defined.