Difference between revisions of "Generics"
From Lazarus wiki
Jump to navigationJump to search (see also section) |
|||
Line 1: | Line 1: | ||
+ | ==Introduction== | ||
+ | |||
+ | Generics are native implementation of class templates. | ||
+ | |||
+ | ==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> | ||
+ | |||
==See also== | ==See also== | ||
* [[Templates]] | * [[Templates]] | ||
* [[Generics proposals]] | * [[Generics proposals]] | ||
+ | |||
+ | ==External links== | ||
+ | |||
+ | * [http://www.freepascal.org/docs-html/ref/refch8.html Free Pascal: Reference guide - Generics] |
Revision as of 15:57, 29 October 2010
Introduction
Generics are native implementation of class templates.
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>