Delphi interface generic function - Is there a work around? -


i have following code

iproxy<t> = interface   ['{1e3a98c5-78ba-4d65-a4ba-b6992b8b4783}']   function setup : isetup<t>;   function proxy : t;   function castas<i : iinterface> : iinterface; end; 

is there way work around compiler error received when compiling?

"[dcc error] delphi.mocks.pas(123): e2535 interface methods must not have parameterized methods"

basically have interface passed around , able cast off it, passing in type cast , having type returned. can achieve class, prefer passing around interface.

additional info:

say have following class

tinterfaceproxy<t> = class(tbaseproxy<t>) private type   tproxyvirtualinterface = class(tvirtualinterface)   private     fproxy : tinterfaceproxy<t>;   protected   public     function queryinterface(const iid: tguid; out obj): hresult; override; stdcall;     constructor create(aproxy : tinterfaceproxy<t>; ainterface: pointer; invokeevent: tvirtualinterfaceinvokeevent);   end; private   fvirtualinterfaces : tdictionary<tguid, tproxyvirtualinterface>; protected   function internalqueryinterface(const iid: tguid; out obj): hresult; stdcall;   function queryinterface(const iid: tguid; out obj): hresult; override;   function proxy : t;override;   function castas<i: iinterface> : i; public   constructor create;override;   destructor destroy;override; end; 

castas works here newly requested cast can created new virtual interface. if want pass class around fine. if require interface i.e. tinterfaceproxy<t> = class(tbaseproxy<t>, iproxy<t>) doesn't work understand that. don't agree it, understand.

therefore how around restriction can call castas function, pass in type (any interface type start with), , able create virtual interface it?

interfaces not support generic parameterized methods, compiler says.

there no workaround because it's fundamental limitation. parameterized methods in classes implemented adding 1 method per instantiation class. works classes since concrete, not viable interfaces. that's because interfaces table of functions , size of table cannot vary depending on generic method instantiations happen present elsewhere in code. similar reasons, generic methods cannot virtual or dynamic.

the code in question little mis-leading also. wrote:

function castas<i : iinterface> : iinterface; 

but i'm sure meant:

function castas<i : iinterface> : i; 

in case, it's not possible. 1 option use class instead. agree bind.

if want in interface, best can is:

function castas(const iid: tguid): iinterface; 

but you'd have call this:

myintf := proxyintf.castas(imyintf) imyintf; 

which feels foul.

choose poison!


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -