mauro russo wrote:
> is there a way in Delphi to write something similar to C++ templates?

Delphi 2007 gives you generics in .Net; Delphi 2009 gives you generics 
in Win32. You get generic interfaces, classes, records, and functions.

For something more like templates, you have to do it yourself. In 
particularly, you would have to instantiate each specialization 
yourself. To do the specialization, you would create a new unit for each 
class (or group of related classes) specialized on a given type. It 
would go like this:

unit IntStringMap;

interface

type
   K = Integer;
   V = string;

{$I Map.interface.pas}

type
   TIntStringMap = MAP;

implementation

{$I Map.implementation.pas}

end.

The code includes two other files that define the interface and 
implementation of an associative-array class named MAP, and it refers to 
two other types as K and V. The code before the first inclusion 
determines what "K" and "V" will mean for the rest of the unit. THe 
first included file could look like this:

type
   MAPENTRY = record
     Key: K;
     Value: V;
   end;

   MAP = class
   private
     FData: array of MAPENTRY;
     procedure SetValue(Key: K; NewValue: V);
     function GetValue(Key: K): V;
   public
     property Values[const Key: K]: V read GetValue write SetValue; default;
   end;

-- 
Rob

Reply via email to