Учитывая приведенный ниже код, который является очень урезанной версией фактического кода, я получаю следующее ошибка:
[Ошибка DCC] Unit3.pas (31): E2010 Несовместимые типы: 'IXList
В функции FindAll .
Я не могу понять, почему, поскольку с предыдущей очень похожей функцией нет проблем.
Может кто-нибудь пролить свет на это?
Это я или ошибка в компиляторе?
unit 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;
С тремя небольшими изменениями (IInterface, FindAll с "S: class" [Спасибо, Мейсон] и приведением типов в 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.
Это определенно похоже на ошибку компилятора. Они говорят, что они приложили много усилий для решения проблем с универсальными шаблонами для следующей версии Delphi XE. Когда он будет выпущен, что должно произойти в течение следующих нескольких недель, загрузите предварительный просмотр и посмотрите, скомпилируется ли он сейчас. Если нет, попробуйте отправить отчет об ошибке в QC.
Кроме того, FindAll
, возможно, следует объявить как function FindAll
. Это не исправляет ошибку, но работающий компилятор, вероятно, выдаст вам ошибку. ;