给定以下代码,这是实际代码的非常简化版本,我收到了以下错误信息:
[DCC Error] Unit3.pas(31): E2010 Incompatible types: 'IXList<Unit3.TXList<T>.FindAll.S>' and 'TXList<Unit3.TXList<T>.FindAll.S>'
在 FindAll 函数中。
我真的看不出来为什么,因为之前非常相似的函数没有问题。
有人能解释一下吗? 是我的问题还是编译器的 bug?
单位 Unit3。
感谢您的回答!看起来是编译器错误,但有一个可接受的解决方法可用。
接口声明如下:
"findall"的实现方式是什么?
我在一个简单的示例中让它工作了。
做类似这样的事情:
[DCC Error] Unit3.pas(31): E2010 Incompatible types: 'IXList<Unit3.TXList<T>.FindAll.S>' and 'TXList<Unit3.TXList<T>.FindAll.S>'
在 FindAll
我真的看不出来为什么,因为之前非常相似的函数没有问题。
有人能解释一下吗? 是我的问题还是编译器的 bug?
单位 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;
"findall"的实现方式是什么?
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;