How to implement two interfaces that have methods with the same name?

I am trying to declare a user list of interfaces from which I want to inherit in order to get a list of specific interfaces (I know about IInterfaceList, this is just an example). I use Delphi 2007, so I do not have access to the actual generics (sorry for 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 GetFirst declaration differs from declaration in interface ISpecificInterfaceList

I suppose that theoretically I can use TCustomInterfaceList, but I do not want to throw GetFirst every time I use it. My goal is to have a specific class that inherits the behavior of the base class and wraps "GetFirst".

How can i achieve this?

Thanks!

+5
source share
3 answers

ISpecificInterfaceList defines three methods. It:

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

Since your two functions have the same name, you need to help the compiler determine which one is.

Use the method resolution clause .

 TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList) public function GetFirstSpecific: ISpecificInterface; function ISpecificInterfaceList.GetFirst = GetFirstSpecific; end; 
+5
source

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 resolve naming conflicts.

+1
source

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

The mapping of an interface function is quiet if you have subsequent descendants implementing the descendant interface because these functions are not passed to the next class as an interface method, so you need to reassign them.

0
source

Source: https://habr.com/ru/post/1211022/


All Articles