[fpc-devel] Generics

Bram Kuijvenhoven kuifwaremailinglists at xs4all.nl
Fri Aug 17 12:35:28 CEST 2007


Florian Klaempfl wrote:
> Mattias Gaertner schrieb:
>> For example, this is currently not allowed:
>>
>>   generic TTree<T> = class(TObject)
>>   type public     TTreeNode = specialize TNode<T>;
>>   end;
>>
>> And this neither:
>>
>>   generic TTree<T> = class(TObject)
>>   type public
>>     TTreeNode = class
>>       Data: T;
>>     end;
>>   end;
>>
>>
>> OTOH records and pointers are allowed.
>> Will this stay, or is this just not yet implemented?
> 
> Is there a need to support this?

Imho the example given by Mattias demonstrates that a similar construct would be desirable; or is there another way to accommodate for the aggregation of generic classes?

I think TTree/TNode is a typical example where you'd like to use the generic type used for TTree to create the corresponding TNode.

type
  generic TNode<T> = class
    type TChildList = TList<TNode>; // and how about this?
    Data:T;
    Children:TChildList;
  end;

  generic TTree<T> = class
    type TTreeNode = specialize TNode<T>;
    Root:TTreeNode;
    GetNode(Index:integer):TTreeNode;
    GetNodeData(Index:integer):T;
  end;

...

function TTree.GetNodeData(Index:integer):T;
begin
  Result:=GetNode(Index).Data;
end;


Regards,

Bram



More information about the fpc-devel mailing list