Search code examples
delphigenericsdelphi-10.2-tokyo

How can I create a list for a generic interface in Delphi 10.2?


I would like to define a list type for a generic interface. It is needed for a tree implementation that stores generic typed data. Unfortunately, the trivial solution does not work:

uses
  Generics.Collections;

type
  ITreeNode<T> = interface;

  TTreeNodeList<T> = TList<ITreeNode<T>>;

  ITreeNode<T> = interface
    ['{BC384FDB-4509-44D3-8946-E7ECD4417C4D}']
    //...
    function getChildNodes : TTreeNodeList<T>;
    function getData : T;
  end;

  TTreeNode<T> = class ( TInterfacedObject, ITreeNode<T> )
    //...
  end;

procedure foo;
var
  node : ITreeNode<cardinal>;
begin
  node := TTreeNode<cardinal>.create;
  //...
end;

Is there any trick to implement it?


Solution

  • OK. I found the solution:

    TTreeNodeList<T> = class ( TList<ITreeNode<T>> )
    end;
    

    and not

    TTreeNodeList<T> = TTreeNodeList<ITreeNode<T>>;