chkaufmann 17 Posted July 25, 2019 Hi, http://docwiki.embarcadero.com/RADStudio/Rio/en/Overview_of_Generics here it says, that this is not possible: IItem = interface function ChildItems<I: IInterface>: IEnumerable<I>; end; All I found is this:https://stackoverflow.com/questions/16533131/delphi-interface-generic-function-is-there-a-work-around so not really nice (readable) solutions. Does anybody have other suggestions? Christian Share this post Link to post
Remy Lebeau 1394 Posted July 26, 2019 23 hours ago, chkaufmann said: http://docwiki.embarcadero.com/RADStudio/Rio/en/Overview_of_Generics here it says, that this is not possible: IItem = interface function ChildItems<I: IInterface>: IEnumerable<I>; end; The DocWiki is correct. Generics are not supported on interface methods. Nor does it make sense to allow that anyway. The main reasons to use interfaces are polymorphism and predictable functionality contracts. If two classes implement the same interface, but they implement the same methods using different parameter/return types, then polymorphism and contracts go out the window. There is no viable use-case where allowing Generics on interface methods makes sense. 23 hours ago, chkaufmann said: Does anybody have other suggestions? No. Why do you think you need this? You don't. Find another design. 1 Share this post Link to post
Stefan Glienke 2002 Posted July 29, 2019 (edited) On 7/26/2019 at 7:57 PM, Remy Lebeau said: Nor does it make sense to allow that anyway. It does. The reason it's not supported is because Delphi Interfaces are COM based which restricts this. A generic type parameter on a method is just that: a parameter - but resolved at compiletime to provide type safety. You probably have never used any language where interfaces easily support methods with generic type parameters - otherwise you would not have this opinion. FWIW this is one of the reasons I so strongly wish for interface helpers because then it would be possible to add such methods via helper - the COM based interface itself is not affected but you can easily augment them with such methods. In 2014 I wrote some code that shows how such interface helper methods could be invoked similar to record helpers: https://pastebin.com/acp2i645 Edited July 29, 2019 by Stefan Glienke 4 Share this post Link to post