[fpc-pascal] Traits Proposal

Sven Barth pascaldragon at googlemail.com
Tue Feb 16 07:36:39 CET 2021


Am 15.02.2021 um 18:08 schrieb Ryan Joseph via fpc-pascal:
>
>> On Feb 15, 2021, at 9:08 AM, Marcos Douglas B. Santos via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>
>> As I understand, this is not a method hiding, but just to tell the
>> compiler which method to implement the interface—the interface could
>> have method names which are very very common to use and this syntax
>> allows us to "rename" those methods without changing the original
>> names that the class might have.
> yeah you may be right, that the interface object itself won't be hookup correctly if this doesn't happen. Seems strange you have 2 declarations for the same method though.
>
>    procedure IMyIntf.Test2 = MyTest2;
>    propcedure MyTest2;
>
>
> instead of something like:
>    
>    propcedure MyTest2 override  IMyIntf.Test2;

Because the first one is much more powerful. As Marco said, you can 
reference a method in the parent class. Also you can redirect different 
interface methods to the same method:

=== code begin ===

type
   IMyIntf1 = interface
     procedure Foo;
   end;

   IMyIntf2 = interface
     procedure Bar;
   end;

   TMyClass = class(TInterfacedObject, IMyIntf1, IMyIntf2)
   public
     procedure IMyIntf1.Foo = Foobar;
     procedure IMyIntf2.Bar = Foobar;

     procedure Foobar;
   end;

=== code end ===

Regards,
Sven


More information about the fpc-pascal mailing list