delphi - 在类正在实现的泛型接口(interface)中使用在泛型类中声明的嵌套类型

标签 delphi generics nested generic-interface

我有一个通用的类似列表的类 tMyList<B>使用方法 Each() 迭代每个元素并调用匿名过程 tMyList<A>.enumProc with paramenter - 当前类型 <B> 的项目。 我想将该类实现为接口(interface),以便更轻松地进行生命周期管理。

问题是我无法在 iMyList<A> 中声明 Each 方法接口(interface),因为tMyList<A>.enumProc类型未知。据我所知接口(interface)不支持嵌套类型?

这是代码:

  tMyList<B> = class;

  iMyList<A> = interface
    procedure each(enumProcedure: iMyList<A>.enumProc); // ERROR - Undeclared identifier: 'enumProc'
  end;

  tMyList<B> = class(tInterfacedObject, iMyList<B>)
    type
      enumProc = reference to procedure(item: iMyList<B>);
    public
      procedure each(enumProcedure: enumProc);
  end;

* 在这种特殊情况下,实现枚举器不是一个选项

最佳答案

完成这项工作的唯一方法是在实现类之外定义过程类型。像这样:

type
  IMyIntf<A> = interface;

  TMyProc<A> = reference to procedure(Intf: IMyIntf<A>);

  IMyIntf<A> = interface
    procedure Foo(Proc: TMyProc<A>);
  end;

  TMyClass<A> = class(TInterfacedObject, IMyIntf<A>)
    procedure Foo(Proc: TMyProc<A>);
  end;

关于delphi - 在类正在实现的泛型接口(interface)中使用在泛型类中声明的嵌套类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677084/

相关文章:

delphi - 如何创建运行时 TCalendarEdit?

ios - Swift 泛型 Bool 不可转换为 Bool

Delphi ActionMainMenuBar 类别位置

delphi - 调试时如何检查非泛型TObjectList的内容?

java - 为 flink 输出流提供类型提示的未弃用方法是什么?

html - 解开 HTML 字符串中不必要的嵌套 div (NodeJs)

python - 检查嵌套的字典值?

Java-instanceof 的替代品?

delphi - delphi创建表单时出错

c - C 中的通用列表操作函数?