[fpc-pascal] Traits Proposal

Ryan Joseph genericptr at gmail.com
Thu Feb 18 23:58:08 CET 2021



> On Feb 18, 2021, at 3:07 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> 
>> 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.

Then this is the real challenge to implement a "default implements property" and something I know nothing about. :) The rest is pretty easy actually though.

> 
>> 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.

Maybe it's a bad example but I'm trying to illustrate how method resolution could be used for overriding (or what ever it would be called in this context.

I know this isn't correct syntax but it's more of this direction:

procedure Draw =  ICircle.Draw;

TShape.Draw is resolved by ICircle.Draw, which is implemented via TCircle. It's quasi-overriding but the VMT table should point to m_circle.Draw so Draw could be called on TShape and get property resolved by the "implements ICircle" property. Hope that makes sense.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list