例如:
type TFoo = class function Pair<T>(e1, e2: T): TList<T>; end;
用aFoo.Pair(1,2)调用它;工作得很好,但是当我将参数签名更改为泛型类型时
type TFoo = class function InsertInto<T>(aList: TList<T>; aVal: T): TList<T>; end;
并试着打电话给它
aFoo.InsertInto(TList< String> .Create,’bar’);
然后编译器抱怨它:
E2010不兼容的类型:’Generics.Collections.TList< uTest.TFoo.InsertInto.T>‘和’Generics.Collections.TList< System.String>‘
有什么方法可以编写这个(或类似的)方法,以便客户端不必指定类型参数?
aFoo.InsertInto< String>(TList< String> .Create,’bar’);
就像在这个例子中Int1和Int2不是同一类型一样:
var Int1: array[1..10] of Integer; Int2: array[1..10] of Integer; ... Int1 := Int2; // <== BOOM! E2008 Incompatible types (in XE2)
实际问题不是类型推断,而是根据Pascal / Delphi的严格规则,类型不兼容.