Difference between revisions of "Generics"
From Lazarus wiki
Jump to navigationJump to searchLine 1: | Line 1: | ||
==Introduction== | ==Introduction== | ||
− | Generics are native implementation of class templates. | + | Generics are native implementation of class templates. Generics are sometimes called parametrized types. FPC have official support for generics since version 2.2. |
==Examples== | ==Examples== | ||
Line 20: | Line 20: | ||
Items[Length(Items) - 1] := Value; | Items[Length(Items) - 1] := Value; | ||
end;</delphi> | end;</delphi> | ||
+ | |||
+ | Generic class can be simply specialized for particular type by use '''specialize''' keyword. | ||
+ | <delphi>Type | ||
+ | TIntegerList = specialize TList<Integer>; | ||
+ | TPointerList = specialize TList<Pointer>; | ||
+ | TStringList = specialize TList<string>;</delphi> | ||
+ | |||
+ | ==fgl unit== | ||
+ | |||
+ | fgl unit is prototype unit for base system generic classes. So faw it contains few basic classes: | ||
+ | * TFPGList | ||
+ | * TFPGObjectList | ||
+ | * TFPGInterfacedObjectList | ||
+ | * TFPGMap | ||
+ | |||
==See also== | ==See also== |
Revision as of 17:18, 29 October 2010
Introduction
Generics are native implementation of class templates. Generics are sometimes called parametrized types. FPC have official support for generics since version 2.2.
Examples
Generic class is defined using keyword generic before class name and use in class declaration: <delphi>type
generic TList<T> = class Items: array of T; procedure Add(Value: T); end;</delphi>
Example of generic class implementation: <delphi>implementation
procedure TList.Add(Value: T); begin
SetLength(Items, Length(Items) + 1); Items[Length(Items) - 1] := Value;
end;</delphi>
Generic class can be simply specialized for particular type by use specialize keyword. <delphi>Type
TIntegerList = specialize TList<Integer>; TPointerList = specialize TList<Pointer>; TStringList = specialize TList<string>;</delphi>
fgl unit
fgl unit is prototype unit for base system generic classes. So faw it contains few basic classes:
- TFPGList
- TFPGObjectList
- TFPGInterfacedObjectList
- TFPGMap