简体   繁体   中英

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?

OK. I found the solution:

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

and not

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM