Output Type Based on the Type of General Arguments (Delphi)

I am trying to write a universal function that accepts the appropriate parameter types.
Delphi makes the correct type parameter in a simple argument to simple arguments.

eg:

type TFoo = class function Pair<T>(e1, e2: T): TList<T>; end; 

calling this with aFoo.Pair(1, 2); works fine, but when I change the parameter signature to a generic type

 type TFoo = class function InsertInto<T>(aList: TList<T>; aVal: T): TList<T>; end; 

and try calling him
aFoo.InsertInto(TList<String>.Create, 'bar');

then the compiler complains about this:
E2010 Incompatible types: 'Generics.Collections.TList<uTest.TFoo.InsertInto.T>' and 'Generics.Collections.TList<System.String>'

Is there any way to write this (or similar) method so that the client does not have a type parameter specification?
aFoo.InsertInto<String>(TList<String>.Create, 'bar');

+7
source share
1 answer

I guess this comes from the strongly typed nature of Delphi.
uTest.TFoo.InsertInto.T equivalent to System.String , but actually a different type .

As in this example, where Int1 and Int2 not of the same type:

 var Int1: array[1..10] of Integer; Int2: array[1..10] of Integer; ... Int1 := Int2; // <== BOOM! E2008 Incompatible types (in XE2) 

The real problem is not with the type of output, but with types that are not compatible with strict Pascal / Delphi rules.

+5
source

All Articles