Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-11

Interfaces inheritance and "Supports" routine

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vladimir Zhirov

Hi,

I've got a question on how does "Supports" function work. Here is the quote from the FPC docs:

Check whether a class or given interface supports an interface. Supports checks whether Instance supports the interface identified by IID. It returns True if it is supported, False. Optionally, a pointer to the interface is returned to Intf.

Suppose I have the code like this:

Show quoted text 17 lines
IMyBasicInterface = interface ['{B6F77975-0C46-4B09-9682-CE96C4CA465A}'] procedure DoSomething; end; IMyExtendedInterface = interface(IMyBasicInterface) ['{DF214D0F-7B92-487F-B21A-4698B1E044AC}'] procedure DoSomethingElse; end; TMyClass = class(IInterfacedObject, IMyExtendedInterface) procedure DoSomething; procedure DoSomethingElse; end; var MyClassInstance: TMyClass;

The following call returns True as expected:

Supports(MyClassInstance, IMyExtendedInterface);

But this one returns False, that seems strange for me:

Supports(MyClassInstance, IMyBasicInterface);

"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?
2) Are there any detailed documentation to read on this subject? I have not found anything, "Supports" is not very google-friendly name :(

Thanks in advance.

---
Vladimir.

Graeme Geldenhuys
to Vladimir Zhirov
Vladimir Zhirov wrote 9 lines
"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/

Graeme Geldenhuys
to Vladimir Zhirov: I've got a question on how does "Supports" function work. Here is the quote from the FPC…
Vladimir Zhirov wrote
So the questions are: 1) Is this an expected behavior? 2) Are there any detailed documentation to read on this subject? I have not found anything, "Supports" is not very google-friendly name :(

http://dn.codegear.com/article/29779

From this article it seems things have changed since Delphi 8 onwards.

The last Delphi version I used was D7. :-) I don't know what delphi version FPC's features are based on.

Regards,
- Graeme -

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

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
On Sun, Nov 2, 2008 at 6:31 PM, Vladimir Zhirov wrote: > So the questions are: > 1) Is this an expected behavior? > 2) Are there any detailed documentation to read on this subject? I have > not found anything, "Supports" is not very google-friendly name :( http://dn.codegear.com/article/29779 >From this article it seems things have changed since Delphi 8 onwards. The last Delphi version I used was D7. :-) I don't know what delphi version FPC's features are based on.

D5 or D7.

All Delphis between D7 and D2007 were not real Delphis :-)

Michael.