[fpc-pascal] Interfaces inheritance and "Supports" routine

Graeme Geldenhuys graemeg.lists at gmail.com
Sun Nov 2 18:23:08 CET 2008


On Sun, Nov 2, 2008 at 6:31 PM, Vladimir Zhirov <vvzh.lists at gmail.com> wrote:
>
> "Supports" does not seem to take into account interfaces inheritance,
> so to make the second call work as expected, I have to change TMyClass
> declaration to
>
>> TMyClass = class(IInterfacedObject, IMyBasicInterface, IMyExtendedInterface)
>
> So the questions are:
> 1) Is this an expected behavior?


I'm not an expert with Interfaces, but as far as I understood the
documentation and from what I have seen in other code, that is
expected behaviour.  Below is information from the Kylix 3 help:

-----------------------[ kylix 3 help ]-------------------------

 An interface-type expression cannot reference an object whose class
implements a descendant interface, unless the class (or one that it
inherits from) explicitly implements the ancestor interface as well.

For example,
type
  IAncestor = interface
  end;
  IDescendant = interface(IAncestor)
    procedure P1;
  end;
  TSomething = class(TInterfacedObject, IDescendant)
    procedure P1;
    procedure P2;
  end;
  ...
var
  D: IDescendant;
  A: IAncestor;
begin
  D := TSomething.Create;  // works!
  A := TSomething.Create;  // error
  D.P1;  // works!
  D.P2;  // error
end;

In this example,
 A is declared as a variable of type IAncestor. Because TSomething
does not list IAncestor among the interfaces it implements, a
TSomething instance cannot be assigned to A. But if we changed
TSomething's declaration to
TSomething = class(TInterfacedObject, IAncestor, IDescendant)
 ...the first error would become a valid assignment.
-----------------------[ end ]-----------------------------


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list