[fpc-pascal] Interfaces via implements keyword

Denis Golovan d.golovan.ml at gmail.com
Wed Nov 25 20:59:02 CET 2009


Thanks for info.

Well. Now I see the technical reason for it.
I thought so too.

Sure, I'm not a guru in compiler construction, but I'd like to know.

Is this a logic by design that I easily get into a situation when querying
interfaces is not "transitive".
I mean, having one of class interfaces I get another interface using which I
cannot query the first one anymore.

Doesn't it defeat the purpose of interfaces? Seems like instead of staying
away of class internals, I am forced to know the underlying details.

See example below. The same commenting again gives another results.
The last Print call just says that interface is not supported (btw, in
Delphi also) if implemented using "implements".

type
  IIntf1 = interface
    ['{925B72C3-B47D-432A-AED8-544B3306055B}']
  end;

  IIntf2 = interface
    ['{E5C9BAC0-CE6D-42A8-BC85-F0AA51360E83}']
    procedure Print;
  end;

  { TClassBase }

  TClassBase = class(TInterfacedObject{, IIntf2})
    Int:Integer;

    procedure Print;
  end;

  { TClass1 }

  TClass1 = class(TClassBase, IIntf1)
    public
      constructor Create;
  end;

  { TClass2 }

  TClass2 = class(TClassBase, IIntf1, IIntf2)
  private
    FIntf: IIntf1;
  public
    constructor Create;

    procedure Print;
    property Intf:IIntf1 read FIntf implements IIntf1; //COMMENT THIS LINE
  end;

{ TClassBase }

procedure TClassBase.Print;
begin
  WriteLn('TClassBase.Print ', Int);
end;

constructor TClass2.Create;
var O:TClass1;
begin
  Int:=3;

  O:=TClass1.Create;
  O.Int:=30;
  FIntf:=O;
end;

{ TClass1 }

constructor TClass1.Create;
begin
  Int:=1;
end;

procedure TClass2.Print;
begin
  WriteLn('TClass2.Print ', Int);
end;

var C2:TClass2;
    I1:IIntf1;
    I2:IIntf2;
begin
  try
    C2:=TClass2.Create;

    C2.Print;

    I1:=C2 as IIntf1;

    I2:=C2 as IIntf2;
    I2.Print;

    I2:=I1 as IIntf2;
    I2.Print;
  except
    on E:Exception do
      WriteLn(E.Message);
  end;

  readln;
end.

On Wed, Nov 25, 2009 at 9:36 AM, Andrew Hall <andrew.hall at shaw.ca> wrote:

> This code is working correctly/logically - and the same as it does in
> Delphi...
>
> Regards,
>
> Andrew Hall.
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



-- 
Best regards,
Denis Golovan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20091125/4b34e07f/attachment.html>


More information about the fpc-pascal mailing list