gpt4 book ai didi

delphi - 为什么我收到 E2531 "Method requires explicit type argument"

转载 作者:行者123 更新时间:2023-12-01 19:11:24 25 4
gpt4 key购买 nike

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

编译器抛出错误:

E2531 Method 'CreateBaseItem' requires explicit type arguments

on the line:

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

但据我所知,实例化 foo1通过foo4应该都是本质上相同的。这个完整的程序突出了这个问题:

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 的通用参数.

最佳答案

这似乎确实是一个编译器错误。 TDescendentFunctionsGroup1 之间的区别和TDescendentFunctionsGroup2是前者是派生自 TFunctionsGroup<TSomeDescendent> 的新类后者是 TFunctionsGroup<TSomeDescendent> 的别名.

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

无论如何,我真的不确定别名会给您带来什么好处,所以我只是这样写:

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

关于delphi - 为什么我收到 E2531 "Method requires explicit type argument",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com