[fpc-pascal] Traits Proposal

Sven Barth pascaldragon at googlemail.com
Tue Feb 16 07:33:37 CET 2021


Am 15.02.2021 um 16:52 schrieb Ryan Joseph via fpc-pascal:
>
>> On Feb 14, 2021, at 11:43 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>
>> Same names should be rejected. Providing a new implementation can be controlled using the interface alias:
>>
>> === code begin ===
>>
>> type
>>    TMyClass = class(TInterfacedObject, IMyIntf)
>>    private
>>      fMyRecord: TMyRecord;
>>    public
>>      procedure IMyIntf.Test2 = MyTest2;
>>      // this will hoist all methods except Test2 from TMyRecord and MyTest2 as Test2 from the class itself
>>      property MyRecord: TMyRecord read fMyRecord implements IMyIntf; default;
>>      propcedure MyTest2;
>>    end;
> So "procedure IMyIntf.Test2 = MyTest2;" basically tells the compiler to ignore the duplicate name resections and then when it encounters the MyTest2 there is no error?

No, it tells the compiler to fullfill the IMyIntf.Test2 requirement by 
using TMyClass.MyTest2 instead of TMyRecord.Test2. This way you can 
control which methods are from the delegate and which might be specific 
to the container class. It also allows to differentiate if you have two 
interfaces with the same method:

=== code begin ===

type
   IMyIntf1 = interface
     procedure Test;
   end;

   IMyIntf2 = interface
     procedure Test;
   end;

   TMyClass = class(TInterfacedObject, IMyIntf1, IMyIntf2)
   public
     procedure IMyIntf1.Test = Foobar;
     procedure Test; // for IMyIntf2
     procedure Foobar; // for IMyIntf1
   end;

=== code end ===


> A duplicate name in this context is basically just method hiding (like in normal OOP) so you could argue it should be allowed. If the property came before then the interface would hide the local declaration. Not too different from how normal inheritance works really.

It should only hide if the method in question is from a parent class. 
For the declarations in the same class there should be errors, cause the 
implementor should know better than to introduce potentially ambigious 
situations.

Regards,
Sven


More information about the fpc-pascal mailing list