[Lazarus] Generics type as parameter

Leonardo M. Ramé l.rame at griensu.com
Sat May 12 13:32:26 CEST 2012


I'm trying to work in an abstract way with generics, I need to create a
method receiving a un-specialized generic and do some work on it.

Example:

function ProcessGeneric(AList: TFPGList);
begin
  ... do something ...
end;

Imagine your application uses many lists of specialized objects, such as
TCustomer, TBooks, TColors. To create a list of those types, you'd do
something like this:

  TCustomers = specialize TFPGList<TCustomer>;
  TBooks = specialize TFPGList<TBook>;
  TColors = specialize TFPGList<TColor>;

Then, you need to pass any of those types to "ProcessGeneric":

var
  lCustomers: TCustomers;
  lBooks: TBooks;
  lColors: TColors;

begin
  ProcessGeneric(lCustomers);
  ProcessGeneric(lBooks);
  ProcessGeneric(lColors);
end;

Without generics, I can use, for example, a TObjectList, or a
TCollection, without any problem. Is there a way to do this using
Generics?.

Regards,
-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Lazarus mailing list