7

I am trying to declare a custom list of interfaces from which I want to inherit in order to get list of specific interfaces (I am aware of IInterfaceList, this is just an example). I'm using Delphi 2007 so I don't have access to actual generics (pity me).

Here is a simplified example:

   ICustomInterfaceList = interface
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   TCustomInterfaceList = class(TInterfacedObject, ICustomInterfaceList)
   public
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   ISpecificInterface = interface(IInterface)
   end;

   ISpecificInterfaceList = interface(ICustomInterfaceList)
      function GetFirst: ISpecificInterface;
   end;

   TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList)
   public
      function GetFirst: ISpecificInterface;
   end;

TSpecificInterfaceList will not compile:

E2211 Declaration of 'GetFirst' differs from declaration in interface 'ISpecificInterfaceList'

I guess I could theoretically use TCustomInterfaceList but I don't want to have to cast "GetFirst" every time I use it. My goal is to have a specific class that both inherits the behavior of the base class and wraps "GetFirst".

How can I achieve this?

Thanks!

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Steve Riley
  • 144
  • 2
  • 10
  • 2
    possible duplicate of [How to map interface names to different method names?](http://stackoverflow.com/questions/1390552/how-to-map-interface-names-to-different-method-names) – RFerwerda Jan 13 '15 at 19:00

3 Answers3

8

ISpecificInterfaceList defines three methods. They are:

procedure Add(AInterface: IInterface);
function GetFirst: IInterface;
function GetFirst: ISpecificInterface;

Because two of your functions share the same name, you need to help the compiler identify which one is which.

Use a method resolution clause.

TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList)
public
  function GetFirstSpecific: ISpecificInterface;
  function ISpecificInterfaceList.GetFirst = GetFirstSpecific;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

Not sure if this is also possible in Delphi7, but you could try using Method Resolution Clauses in your declaration.

function interface.interfaceMethod = implementingMethod;

If possible, this will help you solve the naming conflicts.

RFerwerda
  • 1,267
  • 2
  • 10
  • 24
0

For methods you can also choose override if the parameters are different.

The interface function mapping is quiet difficult if you have later descendants implementing an descendant interface cause these functions are not forwarded to the next class as interface method and so you need to remap them.