在Delphi 2010中返回通用接口的通用方法

时间:2010-08-26 10:50:18

标签: delphi generics interface delphi-2010

鉴于下面的代码,这是一个非常精简的实际代码版本,我收到以下错误:

[DCC错误] Unit3.pas(31):E2010不兼容类型:'IXList< Unit3.TXList< T> .FindAll.S>'和'TXList< Unit3.TXList< T> .FindAll.S>'

在FindAll< S>中功能

我真的不明白为什么因为之前非常类似的功能没有问题。

任何人都可以对此有所了解吗? 是我还是编译器中的错误?

单位Unit3;

interface
uses Generics.Collections;

type
  IXList<T> = interface
  end;

  TXList<T: class> = class(TList<T>, IXList<T>)
  protected
    FRefCount: Integer;
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public
    function Find: IXList<T>;
    function FindAll<S>: IXList<S>;
  end;

implementation
uses Windows;

function TXList<T>.Find: IXList<T>;
begin
  Result := TXList<T>.Create;
end;

function TXList<T>.FindAll<S>: IXList<S>;
begin
  Result := TXList<S>.Create; // Error here  
end;

function TXList<T>.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  Result := E_NoInterface;
end;

function TXList<T>._AddRef: Integer;
begin
  InterlockedIncrement(FRefCount);
end;

function TXList<T>._Release: Integer;
begin
  InterlockedDecrement(FRefCount);
  if FRefCount = 0 then Self.Destroy;
end;

end.

感谢您的回答! 这似乎是一个编译器错误,可以使用可接受的解决方法。

将接口声明为

IXList<T: class> = interface
   function GetEnumerator: TList<T>.TEnumerator;
end;

并将其全部实现为

function TXList<T>.FindAll<S>: IXList<S>;
var
  lst: TXList<S>;
  i: T;
begin
  lst := TXList<S>.Create;
  for i in Self do
    if i.InheritsFrom(S) then lst.Add(S(TObject(i)));

  Result := IXList<S>(IUnknown(lst));
end;

我在一个简单的例子中得到了它。

做类似的事情:

var
  l: TXList<TAClass>;
  i: TASubclassOfTAClass;
begin
.
.
.
for i in l.FindAll<TASubclassOfTAClass> do
begin
   // Do something with i
end;

2 个答案:

答案 0 :(得分:5)

通过三次小修改(IInterface,FindAll with“S:class”[Thanks Mason]和FindAll中的类型转换)我得到了它的编译。

完整代码:

unit Unit16;

interface

uses
  Generics.Collections;

type
  IXList<T> = interface
  end;

  TXList<T: class> = class(TList<T>, IInterface, IXList<T>)
  protected
    FRefCount: Integer;
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public
    function Find: IXList<T>;
    function FindAll<S: class>: IXList<S>;
  end;

implementation
uses Windows;

function TXList<T>.Find: IXList<T>;
begin
  Result := TXList<T>.Create;
end;

function TXList<T>.FindAll<S>: IXList<S>;
begin
  Result := IXList<S>(IUnknown(TXList<S>.Create));
end;

function TXList<T>.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  Result := E_NoInterface;
end;

function TXList<T>._AddRef: Integer;
begin
  InterlockedIncrement(FRefCount);
end;

function TXList<T>._Release: Integer;
begin
  InterlockedDecrement(FRefCount);
  if FRefCount = 0 then Self.Destroy;
end;

end.

答案 1 :(得分:3)

这绝对看起来像编译器错误。他们说他们如何集中精力为下一版Delphi XE改进泛型问题。当它被释放时(应该在接下来的几周内),下载预览并查看它是否会立即编译。如果没有,请尝试使用QC提交错误报告。

此外,FindAll<S>应该声明为function FindAll<S: class>: IXList<S>;。这不能解决错误,但是一个正在运行的编译器可能会给你一个错误。