[fpc-pascal] Traits Proposal

Martin Frb lazarus at mfriebe.de
Thu Feb 11 03:47:01 CET 2021


On 11/02/2021 03:07, Ryan Joseph via fpc-pascal wrote:
> We get the shared namespace
So basically, we get the compiler to automatically create forwarder 
methods for us (or at least that behaviour). And that is all there is. 
right?

In that case IIRC, it was said traits are not allowed constructors. Why?
Now a constructor would and should not have a forwarding method, but 
that the compiler can skip.
Yet, in my code, in the constructor of the TMyClass, I could add
   _traitA.Create
even
   _traitA := TTraitA.Create; // if the trait was not an object, but 
another class.

The compiler can with no doubt create the forwarder methods. And if I 
understand right, then that is all the compiler should do?
(Not that I advertise a trait should be a class / IMHO better not)


> procedure TMyClass.DoThis;
> begin
>    // resolve the conflict by directly referencing the trait
>    traitA.DoThis;
> end;
>
This we already can do. Write our own forwarder method.
I understand it is for conflict resolution only. But see my example => 
as soon as you need to repeat a trait with just a change in name, you 
always need conflict resolution.
IMHO, there are many traits that a class could need more than once.

Also that only works, if a trait is included as a field.
There was at least one message in this mail-thread that suggested
   TMyClass = class(TObject, TTraitA);

The solution is on 
https://en.wikipedia.org/wiki/Trait_(computer_programming)
> /alias/: an operation that creates a new trait by adding a new name 
> for an existing method
Doing that for all methods, means having 2 identical traits, but with 
different names. => no conflict.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210211/0630dc31/attachment.htm>


More information about the fpc-pascal mailing list