delphi - 将自定义比较器传递给 Delphi 中的通用创建过程

标签 delphi generics delphi-10-seattle

我正在尝试使用 Delphi 10 Seattle 并尝试创建我的第一个通用容器类。我需要通用比较器的帮助

这是我创建的一个简单的 Hash 对象:

type
  TsmHeap<T> = class
  private
    fList: TList<T>;
    Comparer: TComparer<T>;
    procedure GetChildren(ParentIndex: integer; var Child1, Child2: integer);
    function GetParent(ChildIndex: integer): integer;
    function GetCapacity: integer;
    function GetCount: integer;
    function MustSwap(iParent, iChild: integer): boolean;
    procedure SetCapacity(const Value: integer);
  public
    constructor Create(aComparer: TComparer<T>); overload;
    constructor Create(aComparer: TCOmparer<T>; aCapacity: integer); overload;

    destructor Destroy; override;

    //-- Methods & Functions
    function Dequeue: T;
    procedure Enqueue(Item: T);
    function IsEmpty: boolean;

    //-- Properties
    property Count: integer read GetCount;
    property Capacity: integer read GetCapacity write SetCapacity;
  end;

我已经为这些方法编写了代码,它可以自行编译,没有任何问题。但是,当我尝试创建该类的整数版本时,我无法对其进行编译。

有问题的代码是:

iHeap := TsmHeap<integer>.Create(TComparer<integer>.Construct(
  function(const Left, Right: integer): integer
  begin
    result := Sign(Left - Right);
  end)
);

这给出了“E2250 没有可以使用这些参数调用的‘Create’的重载版本”

我做错了什么?如何创建比较器?

最佳答案

TComparer<T>.Construct返回 IComparer<T> - 它是一个类函数而不是构造函数。只需更改 TsmHeap<T>.Create 的参数类型即可至 IComparer<T>它应该有效。

关于delphi - 将自定义比较器传递给 Delphi 中的通用创建过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240327/

相关文章:

ios - 如何正确地通过 IPv6 发送电子邮件?

delphi - 串口在delphi中不起作用

java - 反转通用列表?

通用数组的 Swift 错误

java - 为什么泛型方法的定义中有时会省略返回类型之前的尖括号

delphi - 模态按钮在表单上不起作用

delphi - 如何从非delphi应用程序在任务栏上显示delphi dll进度条

regex - 正则表达式到行尾

delphi - 使用RTTI读取和写入枚举属性为Integer

ios - 通过 RAD Studio 10.0 开发的 Firemonkey 应用程序在 iOS 7.1 上崩溃?