【发布时间】:2010-04-25 23:34:50
【问题描述】:
考虑以下 Delphi 2010 中通用实用程序类的声明:
TEnumerableUtils = class
public
class function InferenceTest<T>(Param: T): T;
class function Count<T>(Enumerable: TEnumerable<T>): Integer; overload;
class function Count<T>(Enumerable: TEnumerable<T>; Filter: TPredicate<T>): Integer; overload;
end;
编译器类型推断似乎在这里有问题:
var
I: Integer;
L: TList<Integer>;
begin
TEnumerableUtils.InferenceTest(I); // no problem here
TEnumerableUtils.Count(L); // does not compile: E2250 There is no overloaded version of 'Count' that can be called with these arguments
TEnumerableUtils.Count<Integer>(L); // compiles fine
end;
第一次调用按预期工作,T 被正确推断为 Integer。
第二次调用不起作用,除非我也添加<Integer>——然后它起作用,正如第三次调用所示。我做错了什么还是 Delphi 中的类型推断不支持这一点(我认为这不是 Java 中的问题,这也是为什么期望它也可以在 Delphi 中工作的原因)。
【问题讨论】:
标签: delphi generics type-inference