Difference between revisions of "Generics"

From Free Pascal wiki
Jump to navigationJump to search
m (changed link for fgl unit to point to the reference page)
Line 5: Line 5:
  
 
==fgl unit==
 
==fgl unit==
The easiest way to get started with generics is to use the [[fgl unit]], which is a prototype unit for base system generic classes. So far it contains a few basic classes:
+
The easiest way to get started with generics is to use the [http://freepascal.org/docs-html/current/rtl/fgl/index.html fgl unit], which is a prototype unit for base system generic classes. So far it contains a few basic classes:
 
* TFPGList
 
* TFPGList
 
* TFPGObjectList
 
* TFPGObjectList

Revision as of 19:44, 25 October 2016

English (en) français (fr) 한국어 (ko) polski (pl) русский (ru)

Introduction

The Free Generics Library or FGL is a native implementation of class templates. Generics are sometimes called parametrized types. FPC has had official support for generics since version 2.2.

fgl unit

The easiest way to get started with generics is to use the fgl unit, which is a prototype unit for base system generic classes. So far it contains a few basic classes:

  • TFPGList
  • TFPGObjectList
  • TFPGInterfacedObjectList
  • TFPGMap

Getting Started

The following simple example shows how to store multiple instances of a user defined class in a list:

uses fgl;

type
   TMyClass = class(TObject)
      fld1 : string;
   end;

   TMyList = specialize TFPGObjectList<TMyClass>;

var
   list : TMyList;
   c : TMyClass;

begin
   // create the list and add an element
   list := TMyList.Create;
   c := TMyClass.Create;
   c.fld1 := 'c1';
   list.Add(c);
   // retrieve an element from the list
   c := list[0];

Custom Generic Classes

If the generics defined in the fgl unit do not suit your needs, you may need to define your own generic classes from scratch using the underlying language primitives.

A generic class is defined using the keyword generic before the class name and use in class declaration:

type
  generic TList<T> = class
    Items: array of T;
    procedure Add(Value: T);
  end;

Example of generic class implementation:

implementation

procedure TList.Add(Value: T);
begin
  SetLength(Items, Length(Items) + 1);
  Items[Length(Items) - 1] := Value;
end;

A generic class can be simply specialized for a particular type by using the specialize keyword.

Type  
  TIntegerList = specialize TList<Integer>;
  TPointerList = specialize TList<Pointer>;
  TStringList = specialize TList<string>;

Other Points

1. The compiler parses a generic, but instead of generating code it stores all tokens in a token buffer inside the PPU file.

2. The compiler parses a specialization; for this it loads the token buffer from the PPU file and parses that again. It replaces the generic parameters (in most examples "T") by the particular given type (e.g. LongInt, TObject). The code basically appears as if the same class had been written as the generic but with T replaced by the given type.

Therefore in theory there should be no speed differences between a "normal" class and a generic one.

See also

External links