Inheritance of interfaces with error lazarus / 62-General ---------------------------------------------------------------------- upsidasi · 2022-03-22 12:38 Hi, I stumbled into a couple of compiling error while working with inheritance of interfaces. I have these two interfaces: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Interface1 = interface['{7D5EF8EE-EE23-465D-9A53-5532A199D0D1}']    procedure Proc1();  end;   Interface2 = interface(Interface1)['{83EC551B-B401-48B9-9BA0-CF2878CAB01A}']    procedure Proc2();  end;  and an object that implements Interface2 and therefore Interface1: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TObject2 = class(Interface2)  public    procedure Proc1();    procedure Proc2();  end;  I also defined a function that requires an Interface1 parameter: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetInterface1(const interf1: Interface1);  Here are the calls: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   obj2 := TObject2.Create();   SetInterface1(obj2); // ==> Error: Incompatible type for arg no. 1: Got "TObject2", expected "Interface1"   SetInterface1(Interface1(obj2)); // ==> Error: Class or Object types "TObject2" and "Interface1" are not related   SetInterface1(Interface1(Interface2(obj2))); // ok  As you can see in the comments, the first two raise a compiling error, instead the third one works fine. Therefore, I have a solution but I'd like to know if this is a known issue. You can find attached the related project. Here are my versions:   Lazarus: 2.0.10   FPC: 3.2.0 Thanks! ---------------------------------------------------------------------- Thaddy · 2022-03-22 12:58 AFAIK interface inheritance is only supported by {$mode objfpc} which is silly anyway, because interfaces should not be inherited from by design. Classes can inherit, including its interfaces. You are on a very wrong way. ---------------------------------------------------------------------- Zvoni · 2022-03-22 13:00 With Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TObject1 = class(TInterfacedObject,Interface1)  public    procedure Proc1();  end;                    TObject2 = class(TInterfacedObject,Interface1, Interface2)  public    procedure Proc1();    procedure Proc2();  end;                         Everything compiles ---------------------------------------------------------------------- Thaddy · 2022-03-22 13:03 Yup... Same answer gain ---------------------------------------------------------------------- Thaddy · 2022-03-22 13:04 But you can write helpers for interfaces, though. Forgot to mention. ---------------------------------------------------------------------- upsidasi · 2022-03-22 14:07 Thank you Thaddy and Zvoni for your answers! I'm aware of your solutions but I'm interested in the use of interface inheritance since it seems to be officially supported:   - https://www.freepascal.org/docs-html/ref/refse47.html   - I also find a couple of examples in the Lazarus Handbook (chp 15.3) If we agree on this, I guess we can agree on the fact that the need for a "double casting" is a bit excessive. I just wanted to know if this a known issue and there is any plan of overcoming it in the future. Thanks again! ---------------------------------------------------------------------- Zvoni · 2022-03-22 14:56 Quote from: upsidasi on March 22, 2022, 03:07:14 pmThank you Thaddy and Zvoni for your answers! I'm aware of your solutions but I'm interested in the use of interface inheritance since it seems to be officially supported:   - https://www.freepascal.org/docs-html/ref/refse47.html   - I also find a couple of examples in the Lazarus Handbook (chp 15.3) If we agree on this, I guess we can agree on the fact that the need for a "double casting" is a bit excessive. I just wanted to know if this a known issue and there is any plan of overcoming it in the future. Thanks again! Yes, and on that same page it says: QuoteThe reason for this is that although the class actually implements the methods of IParentInterface, the compiler checks only actually declared interfaces when checking assignment compatibility: all declared interfaces are put in a table and only the contents of this table is checked. Read as: It's by design, and not an Issue ---------------------------------------------------------------------- upsidasi · 2022-03-22 15:21 Thank you Zvoni. I modified my code as follow: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TObject2 = class(Interface1, Interface2)  public    procedure Proc1();    procedure Proc2();  end; ---------------------------------------------------------------------- PascalDragon · 2022-03-23 08:11 Quote from: Thaddy on March 22, 2022, 01:58:11 pmAFAIK interface inheritance is only supported by {$mode objfpc} which is silly anyway, because interfaces should not be inherited from by design. Classes can inherit, including its interfaces. You are on a very wrong way. Interface inheritance is supported in any mode with modeswitch Class enabled which includes both the ObjFPC and Delphi modes. Also inheritance is deeply ingrained in the interface model, because any reference counted interface automatically inherits from IInterface. Interface inheritance aside from IInterface is in fact used in various places in COM, e.g. IClassFactory2 inherits from IClassFactory.