[fpc-pascal] Traits Proposal

Marcos Douglas B. Santos md at delfire.net
Mon Feb 15 17:08:31 CET 2021


On Mon, Feb 15, 2021 at 12:52 PM Ryan Joseph via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:
>
> > 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?
>
> 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.
>

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.

Marcos Douglas


More information about the fpc-pascal mailing list