[fpc-pascal] Generic type declaration example versus manual.

Anthony Walter sysrpl at gmail.com
Mon Nov 23 01:42:45 CET 2009


As a follow up, here are a few generic type declaration examples
lifted directly out of the CodeGear pdf Jonas was thoughtful enough to
link:

*Please Note* These examples do not include generic constraints which
I believe are needed to work effectively with generics. Generic
constraints can be found in the referenced pdf section "3.1.3.3.5
Constraints in Generics" page 626

type
 TFoo1<T> = class(TBar) // Actual type
 end;

 TFoo2<T> = class(TBar2<T>) // Open constructed type
 end;

 TFoo3<T> = class(TBar3<Integer>) // Closed constructed type
 end;

 TMyProc<T> = procedure(Param: T); // procedure type

 TMyProc2<Y> = procedure(Param1, Param2: Y) of object;  // event type

 TRecord<T> = record
   FData: T;
 end;

 IAncestor<T> = interface
   function GetRecord: TRecord<T>;
 end;

 IFoo<T> = interface(IAncestor<T>)
   procedure AMethod(Param: T);
 end;

 TFoo<T> = class(TObject, IFoo<T>)
   FField: TRecord<T>;
   procedure AMethod(Param: T);
   function GetRecord: TRecord<T>;
 end;



More information about the fpc-pascal mailing list