[fpc-pascal] Traits Proposal

Sven Barth pascaldragon at googlemail.com
Thu Feb 18 23:07:40 CET 2021


Am 17.02.2021 um 16:22 schrieb Ryan Joseph via fpc-pascal:
>
>> On Feb 17, 2021, at 6:10 AM, Sven Barth <pascaldragon at googlemail.com> wrote:
>>
>> Simply because no one has come around to implement it yet. The class type case is more complicated than the interface case. (Same would be true for record and objects)
>> So, yes, we'll need to implement this first which would finally bring FPC up to Delphi compatibility regarding interface delegation.
> So "class type method resolution" is what's missing? I never used the interface method resolution so I don't really understand this feature. What needs to happen as far as the compiler is concerned?

The problem is this: if you have a property with an interface type the 
compiler can simply build the interface's VMT for this. However for 
classes (or objects or records) the compiler needs to generate slightly 
different thunks that fixup the correct Self value, especially if things 
are mixed between the subclass and the container class.

> What if you want to implement virtual methods in a base class? This is kind of the core of "composition over inheritance" so it needs to be handled via method resolution I assume.
>
> ========================
>
>
> (*
>   * Shape
>   *)
>
> type
>    TShape = class
>      protected
>        procedure Draw; virtual; abstract;
>    end;
>
> (*
>   * Circle
>   *)
>
> type
>    ICircle = interface
>      procedure Draw;
>    end;
>
> type
>    TCircle = class(ICircle)
>      procedure Draw;
>    end;
>
> (*
>   * My Shape
>   *)
>
> type
>    TMyShape = class(TShape, ICircle)
>      private
>        m_circle: TCircle;
>      protected
>        procedure ICircle.Draw = Draw;
>      public
>        property circle: TCircle read m_circle implements ICircle;
>    end;

Your specific example is rather useless, cause your interface only has a 
single method that you redirect to the class' method thus you wouldn't 
need to use interface delegation. But anyway, your code would generate a 
runtime error, because your TMyShape.Draw would still be abstract.

Regards,
Sven


More information about the fpc-pascal mailing list