Why is Delphi unable to infer the type for a parameter TEnumerable<T>?

Posted by deepc on Stack Overflow See other posts from Stack Overflow or by deepc
Published on 2010-04-25T23:34:50Z Indexed on 2010/04/25 23:43 UTC
Read the original article Hit count: 273

Filed under:
|
|

Consider the following declaration of a generic utility class in 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;

Somehow the compiler type inference seems to have problems here:

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;

The first call works as expected and T is correctly inferred as Integer.

The second call does not work, unless I also add <Integer> -- then it works, as can be seen in the third call. Am I doing something wrong or is the type inference in Delphi just not supporting this (I don't think it is a problem in Java which is why expected it to work in Delphi, too).

© Stack Overflow or respective owner

Related posts about delphi

Related posts about generics