2

In Delphi Seattle 10 I'm trying to delegate the implementation of an interface using the implements keyword, but when an interface derived from the first one is also included in the class declaration, the delegation is not accepted.

Compilation of the code below fails with a "Missing implementation of interface method IInterface1.DoSomethingFor1" message. If I remove the IInterface2 from the class declaration, the code compiles. If I derive IInterface2 from something else then IInterface1 it also compiles.

What am I doing wrong? Or how can I accomplish this?

type
  IInterface1 = interface(IInterface)
    ['{03FB3E2E-C0DD-4E17-B6CD-D333E1E7255E}']
    procedure DoSomethingFor1;
  end;

  Iinterface2 = interface(IInterface1)
    ['{89C266E2-2816-46AD-96AA-DD74E78A4D1E}']
  end;

  T1 = class(TInterfacedObject, IInterface1, IInterface2)
  private
    Fi1: IInterface1;
  public
    property I1_Delegate: IInterface1 read Fi1 implements IInterface1;
  end;
Bascy
  • 2,017
  • 1
  • 21
  • 46
  • Related, but not duplicates: http://stackoverflow.com/q/24705061/224704 http://stackoverflow.com/q/28582426/224704 – Disillusioned May 18 '16 at 11:27
  • As well as those there is an *exact* duplicate :) the answer to which also explains the reason the compiler requires that an implementation of a derived interface also requires implementation (not delegation) of any base interface. – Deltics May 19 '16 at 05:21

1 Answers1

5

You need to explicitly implement both IInterface1 and IInterface2. You only implement the former. Hence the compilation error. Your implementing class should look like this instead:

type
  TImplementingClass = class(TInterfacedObject, IInterface1, IInterface2)
  private
    Fi2: IInterface2;
  public
    property I2_Delegate: IInterface2 read Fi2 implements IInterface1, IInterface2;
  end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I added the IInterface1 implementation to the class declaration thinking it was necessary to delegate the implementation, but seeing this it turns out I can leave out the use of IInterface1 intirely in the class declaration. Implementing Interface2 is enough. – Bascy May 18 '16 at 10:25
  • but the error message is said to be "Missing implementation of interface method **IInterface1**.DoSomethingFor1" not "Missing implementation of interface method **IInterface2**.DoSomethingFor1"... – Arioch 'The May 18 '16 at 16:33