8.1 Introduction

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 ocially has support for templates or Generics. They are implemented as a kind of macro which is stored in the unit les that the compiler generates, and which is replayed as soon as a generic class is specialized.

Currently, only generic classes can be dened. Later, support for generic records, functions and arrays may be introduced.

Creating and using generics is a 2-phase process.

  1. The denition of the generic class is dened as a new type: this is a code template, a macro which can be replayed by the compiler at a later stage.
  2. A generic class is specialized: this denes a second class, which is a specic implementation of the generic class: the compiler replays the macro which was stored when the generic class was dened.