In Delphi 2010, I have defined a generic TInterfaceList as follows:
type
TInterfaceList<I: IInterface> = class(TInterfaceList)
function GetI(index: Integer): I;
procedure PutI(index: Integer; const Item: I);
property Items[index: Integer]: I read GetI write PutI; default;
end;
implementation
function TInterfaceList<I>.GetI(index: Integer): I;
begin
result := I(inherited Get(Index));
end;
procedure TInterfaceList<I>.PutI(index: Integer; const Item: I);
begin
inherited Add(Item);
end;
I haven't had any problems yet, but is there anything inherently risky about doing this? Would it be possible to add an enumerator to it to allow for..in loops to work on it? If there's nothing wrong with it, I wonder why something similar isn't already defined in the RTL.