Difference between revisions of "Generics"

From Free Pascal wiki
Jump to navigationJump to search
(Language)
(language; link to overview page of data structures that include generics-based structures)
Line 1: Line 1:
 
==Introduction==
 
==Introduction==
  
Generics are a native implementation of class templates. Generics are sometimes called parametrized types. FPC have official support for generics since version 2.2.  
+
Generics are a native implementation of class templates. Generics are sometimes called parametrized types. FPC has had official support for generics since version 2.2.  
  
 
==Examples==
 
==Examples==
  
A generic class is defined using keyword '''generic''' before the class name and use in class declaration:
+
A generic class is defined using the keyword '''generic''' before the class name and use in class declaration:
<syntaxhighlight>type
+
<syntaxhighlight>
 +
type
 
   generic TList<T> = class
 
   generic TList<T> = class
 
     Items: array of T;
 
     Items: array of T;
 
     procedure Add(Value: T);
 
     procedure Add(Value: T);
   end;</syntaxhighlight>
+
   end;
 +
</syntaxhighlight>
  
 
Example of generic class implementation:
 
Example of generic class implementation:
<syntaxhighlight>implementation
+
<syntaxhighlight>
 +
implementation
  
 
procedure TList.Add(Value: T);
 
procedure TList.Add(Value: T);
Line 19: Line 22:
 
   SetLength(Items, Length(Items) + 1);
 
   SetLength(Items, Length(Items) + 1);
 
   Items[Length(Items) - 1] := Value;
 
   Items[Length(Items) - 1] := Value;
end;</syntaxhighlight>
+
end;
 +
</syntaxhighlight>
  
Generic class can be simply specialized for particular type by use '''specialize''' keyword.
+
A generic class can be simply specialized for a particular type by using the '''specialize''' keyword.
<syntaxhighlight>Type   
+
<syntaxhighlight>
 +
Type   
 
   TIntegerList = specialize TList<Integer>;
 
   TIntegerList = specialize TList<Integer>;
 
   TPointerList = specialize TList<Pointer>;
 
   TPointerList = specialize TList<Pointer>;
   TStringList = specialize TList<string>;</syntaxhighlight>
+
   TStringList = specialize TList<string>;
 +
</syntaxhighlight>
  
 
==fgl unit==
 
==fgl unit==
Line 39: Line 45:
 
1. The compiler parses a generic, but instead of generating code it stores all tokens in a token buffer inside the PPU file.
 
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, but replaces the generic parameters (in most examples "T") by the particular given type (e.g. LongInt, TObject).
+
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.
 
The code basically appears as if the same class had been written as the generic but with T replaced by the given type.
  
Line 48: Line 54:
 
* [[Templates]]
 
* [[Templates]]
 
* [[Generics proposals]]
 
* [[Generics proposals]]
 +
* [[Data Structures, Containers, Collections]]
  
 
==External links==
 
==External links==

Revision as of 13:16, 15 October 2013

Introduction

Generics are a native implementation of class templates. Generics are sometimes called parametrized types. FPC has had official support for generics since version 2.2.

Examples

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>;

fgl unit

The fgl unit is a prototype unit for base system generic classes. So far it contains a few basic classes:

  • TFPGList
  • TFPGObjectList
  • TFPGInterfacedObjectList
  • TFPGMap

Technical details

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