【发布时间】:2011-04-04 05:07:14
【问题描述】:
鉴于下面的代码,这是实际代码的一个非常精简的版本,我收到以下错误:
[DCC 错误] Unit3.pas(31): E2010 不兼容类型:'IXList 在 FindAll 我真的不明白为什么,因为之前非常相似的功能没有问题。 谁能解释一下? 单元单元3; 感谢您的回答!
这似乎是一个编译器错误,具有可接受的解决方法。 接口声明为 findall 实现为 我在一个简单的例子中得到了它。 做类似的事情: 函数中。
是我还是编译器的bug?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;
【问题讨论】:
标签: delphi generics interface delphi-2010