可能重复:
Generic method returning generic interface in Delphi 2010
在Delphi 2010中,何时ISomeGenericInterface<T>
可分配给ISomeGenericInterface<T>
?
以下程序按预期编译并运行......
unit uGenDemo;
interface
{.$define declare-a-type}
function Tokenize: IEnumerator<string>;
{$IFDEF declare-a-type}
type TX = integer;
{$ENDIF}
implementation
uses SysUtils;
type
IStringEnumerator = interface( IEnumerator<string>)
['{3A28E4FB-CDF7-4933-8D67-D9EF5C2466E0}']
end;
TBaseEnumerator = class( TInterfacedObject, IEnumerator)
private
function MoveNext: Boolean;
function BaseGetCurrent: TObject;
function IEnumerator.GetCurrent = BaseGetCurrent;
procedure Reset;
end;
TStringEnumerator = class( TBaseEnumerator, IStringEnumerator)
private
function GetCurrent: string;
property Current: string read GetCurrent;
end;
function Tokenize: IEnumerator<string>;
begin
result := TStringEnumerator.Create as IStringEnumerator
end;
{ TBaseEnumerator }
function TBaseEnumerator.BaseGetCurrent: TObject;
begin
result := nil
end;
function TBaseEnumerator.MoveNext: Boolean;
begin
result := False
end;
procedure TBaseEnumerator.Reset;
begin
end;
{ TStringEnumerator }
function TStringEnumerator.GetCurrent: string;
begin
result := ''
end;
end.
...但是当我们在声明Tokenize()
函数后声明任何类型时,例如,通过更改...的行
{.$define declare-a-type}
...到......
{$define declare-a-type}
...编译器阻止并抛出错误......
[DCC Error] uGenDemo.pas(43): E2010 Incompatible types: 'IEnumerator<System.string>' and 'IStringEnumerator'
为什么吗
(我的编译器版本是Embarcadero®Delphi®2010版本14.0.3593.25826)