为什么我得到 E2531 "Method requires explicit type argument"



下面的代码非常依赖泛型,我认为这暴露了泛型处理中的一个错误。但也许只是有些事情我不明白。

编译器引发错误:

E2531方法"CreateBaseItem"需要行上的显式类型参数

foo3 := TFactory.CreateBaseItem<TDescendentFunctionsGroup2.Select>;

然而,据我所知,实例化foo1foo4应该基本上是相同的。这个完整的程序突出了问题:

program SO53568763;
type
TBaseItem = class(TInterfacedObject);
IListableItem<T> = interface
['{6FD07ACB-04BB-4BFC-A38C-9B98F86DBC25}']
end;
TSomeDescendent = class(TBaseItem, IListableItem<TSomeDescendent>)
end;
TSelectFunctionsGenerator<T: TBaseItem, IListableItem<T>> = class(TBaseItem)
end;
TFunctionsGroup<T: TBaseItem, IListableItem<T>> = class
public
type
Select = TSelectFunctionsGenerator<T>;
end;
TDescendentFunctionsGroup1 = class(TFunctionsGroup<TSomeDescendent>);
TDescendentFunctionsGroup2 = TFunctionsGroup<TSomeDescendent>;
TFactory = class
public
class function CreateBaseItem<T: TBaseItem>: T;
end;
class function TFactory.CreateBaseItem<T>;
begin
end;
procedure Foo;
var
foo: TSelectFunctionsGenerator<TSomeDescendent>;
foo1: TFunctionsGroup<TSomeDescendent>.Select;
foo2: TDescendentFunctionsGroup1.Select;
foo3: TDescendentFunctionsGroup2.Select;
begin
foo := TFactory.CreateBaseItem<TSelectFunctionsGenerator<TSomeDescendent>>;
foo1 := TFactory.CreateBaseItem<TFunctionsGroup<TSomeDescendent>.Select>;
foo2 := TFactory.CreateBaseItem<TDescendentFunctionsGroup1.Select>;
foo3 := TFactory.CreateBaseItem<TDescendentFunctionsGroup2.Select>;
end;
begin
end.

奇怪的是,TDescendentFunctionsGroup2.Select足够显式,可以声明该类型的变量,但不够显式,不能用作CreateBaseItem的泛型参数。

这似乎是一个编译器错误。TDescendentFunctionsGroup1TDescendentFunctionsGroup2的区别在于前者是从TFunctionsGroup<TSomeDescendent>派生的新类,后者是TFunctionsGroup<TSomeDescendent>的别名。

所以我的猜测是,解析器或编译器在泛型类型的别名方面存在问题。

无论如何,我真的不确定这个别名能给你带来什么好处,所以我只想这样写:

var
foo3: TFunctionsGroup<TSomeDescendent>.Select;
...
foo3 := TFactory.CreateBaseItem<TFunctionsGroup<TSomeDescendent>.Select>;

相关内容

最新更新