Well I've been experimenting a bit. I've made them work to some extend but they seem very rigid(But I've had no funny errors yet)
I've made a small generic TList class including working example. Does anyone have an idea if you could extend this(or recode it) so it would work with normal records and simple types too?
[pascal]
program test;
{$MODE OBJFPC}
uses classes;
type
generic TGList<_T> = class(TList)
//TGList = class(TList)
protected
function Get(Index: Integer): _T;
procedure Put(Index: Integer; Item: _T);
public
function Add(Item: _T): Integer;
function Extract(Item: _T): _T;
function First: _T;
function IndexOf(Item: _T): Integer;
procedure Insert(Index: Integer; Item: _T);
function Last: _T;
function Remove(Item: _T): Integer;
property Items[Index: Integer]: _T read Get write Put; default;
end;
{ TGList }
function TGList.Add(Item: _T): Integer;
begin
result := inherited Add(Item);
end;
function TGList.Extract(Item: _T): _T;
begin
result := _T(inherited Extract(Item));
end;
function TGList.First: _T;
begin
Result := _T(inherited First);
end;
function TGList.Get(Index: Integer): _T;
begin
result := _T(Inherited Get(index));
end;
function TGList.IndexOf(Item: _T): Integer;
begin
result := Inherited IndexOf(item);
end;
procedure TGList.Insert(Index: Integer; Item: _T);
begin
Inherited Insert(index, Item);
end;
function TGList.Last: _T;
begin
Result := _T(inherited Last);
end;
procedure TGList.Put(Index: Integer; Item: _T);
begin
inherited Put(index, Item);
end;
function TGList.Remove(Item: _T): Integer;
begin
result := Inherited Remove(Item);
end;
type
TSalmon = class
name: string[5];
end;
TLakseList = specialize TGList<TSalmon>;
var x: TLakseList;
a,b: TSalmon;
i: integer;
begin
a := TSalmon.Create;
a.name := 'test1';
b := TSalmon.Create;
b.name := 'test2';
x := TLakseList.Create;
x.add(a);
x.add(b);
for I := 0 to x.count - 1 do
begin
writeln(x[i].name);
x[i].free;
end;
x.free;
end.
[/pascal]
Bookmarks