[fpc-pascal] Traits Proposal

Martin Frb lazarus at mfriebe.de
Thu Feb 18 23:34:24 CET 2021


On 18/02/2021 22:42, Sven Barth wrote:
>
> You need to constrain T as a TObject, then it works.

Ah that works. With 2 restrictions

TBase can be any class, since a trait can be applied to any class.
So the only known common base is TObject.

(restriction 1)
But "TBase: TObject" means that the trait can not access Self.Base, even 
if the latter passed in type has such a method.
Same with (note the "class" keyword)
      generic TTrait<TBase: class; THost> = class(TBase)


Normally if the param to a generic is not restricted you can write 
T.anything, and during specialization this will be resolved or fail.
So that ability is lost.

As a workaround THost can be passed in.
(restriction 2)
That requires a forward declaration of TFoo. Otherwise it fails.


type
   TFooBase = class
     procedure Base;
   end;

   generic TTrait<TBase: TObject; THost> = class(TBase)
     procedure Bar;
   end;

   TMyFoo = class;
   TMyFoo = class(specialize TTrait<TFooBase, TMyFoo>)
      procedure Foo;
   end;


procedure TTrait.Bar;
begin
   //Self.Base; // fails
   THost(Self).Base;
   THost(Self).Foo;
end;



More information about the fpc-pascal mailing list