How to pass a generic list as a parameter to generic method












0















I've met a strange Delphi (Tokyo) compiler's restriction when trying to pass an instance of generic list into generic method whereas passing generic array to method (1) compiler accepts and everything works as expected.



(1) class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
(2) class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
(3) class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);


But when passing the same array to the method with signature (2), Delphi complains with error:



[dcc32 Error] Project8.dpr(49): E2010 Incompatible types: 'System.TArray<....TSomeClass.DoSomethingWithGenericArray.T>' and 'System.TArray<....TBaseClass>'


even if TArray<T> is declared as array of <T>.



The same error occurs when passing a generic list to the method (3).



What is the reason for such restriction? But primarily, how to implement such stuff in Delphi? (e.g. in C# there are no such restrictions).



I read some explanations concerning possible inheritance of <T>, but I dont accept such argument, because the same problem can occur with arrays and compiler accepts it:



SomeArray: TArray<TBaseClass>;

SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);


UPDATE: example



program Project8;

uses
System.SysUtils, System.Generics.Collections;

type
TBaseClass = class
end;

TDescendantClass = class(TBaseClass)
end;

TGrandDescendantClass = class(TDescendantClass)
end;

TSomeClass = class
class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);
end;

var
SomeArray: TArray<TBaseClass>;
SomeList: TList<TBaseClass>;

class procedure TSomeClass.DoSomethingWithDynamicArray<T>(AArray: array of T);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericArray<T>(AArray: TArray<T>);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericList<T>(AList: TList<T>);
begin
end;

begin
try
SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);
TSomeClass.DoSomethingWithGenericArray(SomeArray); //E2010: Incompatible type

SomeList := TList<TBaseClass>.Create;
SomeList.AddRange([TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create]);
TSomeClass.DoSomethingWithGenericList(SomeList); //E2010: Incompatible type
except on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.









share|improve this question

























  • Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

    – David Heffernan
    Nov 24 '18 at 12:06











  • @David, I updated question by example (not a SSCCE), because it is not compilable

    – pf1957
    Nov 24 '18 at 12:45
















0















I've met a strange Delphi (Tokyo) compiler's restriction when trying to pass an instance of generic list into generic method whereas passing generic array to method (1) compiler accepts and everything works as expected.



(1) class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
(2) class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
(3) class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);


But when passing the same array to the method with signature (2), Delphi complains with error:



[dcc32 Error] Project8.dpr(49): E2010 Incompatible types: 'System.TArray<....TSomeClass.DoSomethingWithGenericArray.T>' and 'System.TArray<....TBaseClass>'


even if TArray<T> is declared as array of <T>.



The same error occurs when passing a generic list to the method (3).



What is the reason for such restriction? But primarily, how to implement such stuff in Delphi? (e.g. in C# there are no such restrictions).



I read some explanations concerning possible inheritance of <T>, but I dont accept such argument, because the same problem can occur with arrays and compiler accepts it:



SomeArray: TArray<TBaseClass>;

SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);


UPDATE: example



program Project8;

uses
System.SysUtils, System.Generics.Collections;

type
TBaseClass = class
end;

TDescendantClass = class(TBaseClass)
end;

TGrandDescendantClass = class(TDescendantClass)
end;

TSomeClass = class
class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);
end;

var
SomeArray: TArray<TBaseClass>;
SomeList: TList<TBaseClass>;

class procedure TSomeClass.DoSomethingWithDynamicArray<T>(AArray: array of T);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericArray<T>(AArray: TArray<T>);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericList<T>(AList: TList<T>);
begin
end;

begin
try
SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);
TSomeClass.DoSomethingWithGenericArray(SomeArray); //E2010: Incompatible type

SomeList := TList<TBaseClass>.Create;
SomeList.AddRange([TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create]);
TSomeClass.DoSomethingWithGenericList(SomeList); //E2010: Incompatible type
except on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.









share|improve this question

























  • Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

    – David Heffernan
    Nov 24 '18 at 12:06











  • @David, I updated question by example (not a SSCCE), because it is not compilable

    – pf1957
    Nov 24 '18 at 12:45














0












0








0








I've met a strange Delphi (Tokyo) compiler's restriction when trying to pass an instance of generic list into generic method whereas passing generic array to method (1) compiler accepts and everything works as expected.



(1) class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
(2) class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
(3) class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);


But when passing the same array to the method with signature (2), Delphi complains with error:



[dcc32 Error] Project8.dpr(49): E2010 Incompatible types: 'System.TArray<....TSomeClass.DoSomethingWithGenericArray.T>' and 'System.TArray<....TBaseClass>'


even if TArray<T> is declared as array of <T>.



The same error occurs when passing a generic list to the method (3).



What is the reason for such restriction? But primarily, how to implement such stuff in Delphi? (e.g. in C# there are no such restrictions).



I read some explanations concerning possible inheritance of <T>, but I dont accept such argument, because the same problem can occur with arrays and compiler accepts it:



SomeArray: TArray<TBaseClass>;

SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);


UPDATE: example



program Project8;

uses
System.SysUtils, System.Generics.Collections;

type
TBaseClass = class
end;

TDescendantClass = class(TBaseClass)
end;

TGrandDescendantClass = class(TDescendantClass)
end;

TSomeClass = class
class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);
end;

var
SomeArray: TArray<TBaseClass>;
SomeList: TList<TBaseClass>;

class procedure TSomeClass.DoSomethingWithDynamicArray<T>(AArray: array of T);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericArray<T>(AArray: TArray<T>);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericList<T>(AList: TList<T>);
begin
end;

begin
try
SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);
TSomeClass.DoSomethingWithGenericArray(SomeArray); //E2010: Incompatible type

SomeList := TList<TBaseClass>.Create;
SomeList.AddRange([TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create]);
TSomeClass.DoSomethingWithGenericList(SomeList); //E2010: Incompatible type
except on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.









share|improve this question
















I've met a strange Delphi (Tokyo) compiler's restriction when trying to pass an instance of generic list into generic method whereas passing generic array to method (1) compiler accepts and everything works as expected.



(1) class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
(2) class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
(3) class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);


But when passing the same array to the method with signature (2), Delphi complains with error:



[dcc32 Error] Project8.dpr(49): E2010 Incompatible types: 'System.TArray<....TSomeClass.DoSomethingWithGenericArray.T>' and 'System.TArray<....TBaseClass>'


even if TArray<T> is declared as array of <T>.



The same error occurs when passing a generic list to the method (3).



What is the reason for such restriction? But primarily, how to implement such stuff in Delphi? (e.g. in C# there are no such restrictions).



I read some explanations concerning possible inheritance of <T>, but I dont accept such argument, because the same problem can occur with arrays and compiler accepts it:



SomeArray: TArray<TBaseClass>;

SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);


UPDATE: example



program Project8;

uses
System.SysUtils, System.Generics.Collections;

type
TBaseClass = class
end;

TDescendantClass = class(TBaseClass)
end;

TGrandDescendantClass = class(TDescendantClass)
end;

TSomeClass = class
class procedure DoSomethingWithDynamicArray<T: class>(AArray: array of T);
class procedure DoSomethingWithGenericArray<T: class>(AArray: TArray<T>);
class procedure DoSomethingWithGenericList<T: class>(AList: TList<T>);
end;

var
SomeArray: TArray<TBaseClass>;
SomeList: TList<TBaseClass>;

class procedure TSomeClass.DoSomethingWithDynamicArray<T>(AArray: array of T);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericArray<T>(AArray: TArray<T>);
begin
end;

class procedure TSomeClass.DoSomethingWithGenericList<T>(AList: TList<T>);
begin
end;

begin
try
SomeArray := [TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create];
TSomeClass.DoSomethingWithDynamicArray(SomeArray);
TSomeClass.DoSomethingWithGenericArray(SomeArray); //E2010: Incompatible type

SomeList := TList<TBaseClass>.Create;
SomeList.AddRange([TBaseClass.Create, TDescendantClass.Create,
TGrandDescendantClass.Create]);
TSomeClass.DoSomethingWithGenericList(SomeList); //E2010: Incompatible type
except on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.






delphi generics type-inference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 16:17









Remy Lebeau

338k19262456




338k19262456










asked Nov 24 '18 at 11:46









pf1957pf1957

8871517




8871517













  • Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

    – David Heffernan
    Nov 24 '18 at 12:06











  • @David, I updated question by example (not a SSCCE), because it is not compilable

    – pf1957
    Nov 24 '18 at 12:45



















  • Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

    – David Heffernan
    Nov 24 '18 at 12:06











  • @David, I updated question by example (not a SSCCE), because it is not compilable

    – pf1957
    Nov 24 '18 at 12:45

















Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

– David Heffernan
Nov 24 '18 at 12:06





Please show a Minimal, Complete, and Verifiable example. Should be possible in about 15 lines.

– David Heffernan
Nov 24 '18 at 12:06













@David, I updated question by example (not a SSCCE), because it is not compilable

– pf1957
Nov 24 '18 at 12:45





@David, I updated question by example (not a SSCCE), because it is not compilable

– pf1957
Nov 24 '18 at 12:45












1 Answer
1






active

oldest

votes


















2














Delphi wants you to specify the type parameter on the generic method because it has quite limited type inference from closed generic types.



We could call that type erasure, the compiler at this point sees the type of SomeList being TList<TBaseClass> but does not have any knowledge that it is a closed generic type. That means it cannot infer TBaseClass from the passed SomeList and see "Oh, that is a TList<T> with T being TBaseClass".



So you have to write:



TSomeClass.DoSomethingWithGenericList<TBaseClass>(SomeList);


Same applies to the TArray<T> overload.



For array of T which is an open array (array of ... as a parameter type is not a dynamic array!) the compiler knows to infer the type when passing your SomeArray variable.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457792%2fhow-to-pass-a-generic-list-as-a-parameter-to-generic-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Delphi wants you to specify the type parameter on the generic method because it has quite limited type inference from closed generic types.



    We could call that type erasure, the compiler at this point sees the type of SomeList being TList<TBaseClass> but does not have any knowledge that it is a closed generic type. That means it cannot infer TBaseClass from the passed SomeList and see "Oh, that is a TList<T> with T being TBaseClass".



    So you have to write:



    TSomeClass.DoSomethingWithGenericList<TBaseClass>(SomeList);


    Same applies to the TArray<T> overload.



    For array of T which is an open array (array of ... as a parameter type is not a dynamic array!) the compiler knows to infer the type when passing your SomeArray variable.






    share|improve this answer






























      2














      Delphi wants you to specify the type parameter on the generic method because it has quite limited type inference from closed generic types.



      We could call that type erasure, the compiler at this point sees the type of SomeList being TList<TBaseClass> but does not have any knowledge that it is a closed generic type. That means it cannot infer TBaseClass from the passed SomeList and see "Oh, that is a TList<T> with T being TBaseClass".



      So you have to write:



      TSomeClass.DoSomethingWithGenericList<TBaseClass>(SomeList);


      Same applies to the TArray<T> overload.



      For array of T which is an open array (array of ... as a parameter type is not a dynamic array!) the compiler knows to infer the type when passing your SomeArray variable.






      share|improve this answer




























        2












        2








        2







        Delphi wants you to specify the type parameter on the generic method because it has quite limited type inference from closed generic types.



        We could call that type erasure, the compiler at this point sees the type of SomeList being TList<TBaseClass> but does not have any knowledge that it is a closed generic type. That means it cannot infer TBaseClass from the passed SomeList and see "Oh, that is a TList<T> with T being TBaseClass".



        So you have to write:



        TSomeClass.DoSomethingWithGenericList<TBaseClass>(SomeList);


        Same applies to the TArray<T> overload.



        For array of T which is an open array (array of ... as a parameter type is not a dynamic array!) the compiler knows to infer the type when passing your SomeArray variable.






        share|improve this answer















        Delphi wants you to specify the type parameter on the generic method because it has quite limited type inference from closed generic types.



        We could call that type erasure, the compiler at this point sees the type of SomeList being TList<TBaseClass> but does not have any knowledge that it is a closed generic type. That means it cannot infer TBaseClass from the passed SomeList and see "Oh, that is a TList<T> with T being TBaseClass".



        So you have to write:



        TSomeClass.DoSomethingWithGenericList<TBaseClass>(SomeList);


        Same applies to the TArray<T> overload.



        For array of T which is an open array (array of ... as a parameter type is not a dynamic array!) the compiler knows to infer the type when passing your SomeArray variable.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 12:52

























        answered Nov 24 '18 at 12:47









        Stefan GlienkeStefan Glienke

        16.2k13478




        16.2k13478
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457792%2fhow-to-pass-a-generic-list-as-a-parameter-to-generic-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen