[fpc-devel] Templates / Generics Syntax

Micha Nelissen micha at neli.hopto.org
Fri Nov 4 14:16:23 CET 2005


Micha Nelissen wrote:
> <code>
> type
>   TGenericCollection = generic(T: TCollectionItem) class(TComponent)
>   ...implement TCollection and use T
>   end;
> 
>   TCollection = TGenericCollection of (TCollectionItem);
>   TFieldDefs = TGenericCollection of (TFieldDef);
> </code>

So generic procs could look like:

<code>
function generic(T: TTreeItem) MyFunc(A: T): T;
begin
  // do something with the tree item
end;
</code>

My restrictions won't allow implementing generic Max and Min, I guess. 
That really needs macro-alike stuff (for the compiler implementation). 
The syntax could look like:

<code>
function generic(T) Max(A, B: T): T;
begin
   if A < B then
     Result := B
   else
     Result := A;
end;
</code>

Micha




More information about the fpc-devel mailing list