[fpc-pascal] Interface bug or some new feature

Sven Barth pascaldragon at googlemail.com
Sun Jan 6 12:59:21 CET 2019


Am So., 6. Jan. 2019, 11:42 hat denisgolovan <denisgolovan at yandex.ru>
geschrieben:

> Hi all
>
> I've been using 3.1.1 compiler for a long time and now I am trying to
> upgrade to 3.3.1 from trunk.
> However, I am stuck with some new behavior when using classes + interfaces.
> I've managed to reproduce it in a small example which follows.
>
> Specifically 3.1.1 compiler compiles it and correctly prints "Double".
> 3.3.1 compiler refuses to compile it at all.
> Please comment if it's a bug or a new breaking feature.
>
> //==================================================================
> program project1;
>
> {$mode objfpc}{$H+}
>
> uses
>   {$IFDEF UNIX}{$IFDEF UseCThreads}
>   cthreads,
>   {$ENDIF}{$ENDIF}
>   Classes
>   { you can add units after this };
>
> type
>   IIntf1 = interface
>     procedure P(i:Integer);
>   end;
>
>   TClass1 = class(TInterfacedObject, IIntf1)
>     procedure P(i:Integer);
>   end;
>
>   IIntf2 = interface(IIntf1)
>     procedure P(f:Double);
>   end;
>
>   TClass2 = class(TClass1, IIntf2) // Error: No matching implementation
> for interface method "P(LongInt);" found
>     procedure P(f:Double);
>   end;
>
> procedure TClass1.P(i:Integer);
> begin
>   WriteLn('Integer');
> end;
>
> procedure TClass2.P(f:Double);
> begin
>   WriteLn('Double');
> end;
>
> var v2:TClass2;
> begin
>   v2:=TClass2.Create;
>   v2.P(0.0);
>   v2.Free;
> end.
>

The default visibility for classes without $M+ is private. Thus TClass1.P
is private. An interface implementation does not have access to private
methods of a parent class. So you need to declare P as protected for this
to work.
Though to be fair in the specific example you gave I think that it should
work as both are within the same unit. We'll need to check that...

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190106/d7553531/attachment.html>


More information about the fpc-pascal mailing list