[fpc-pascal] Traits Proposal

Martin Frb lazarus at mfriebe.de
Sun Feb 14 18:38:08 CET 2021


On 14/02/2021 18:20, Sven Barth via fpc-pascal wrote:
>
>>>
>>>    TTest = class(TObject, ITest)
>>>    private
>>>      fTest: TTestImpl;
>>>    public
>>>      property Test: TTestImpl read fTest implements ITest; default;
>>>    end;

Well there is a difference there.

Interfaces are essentially inheritance. One can access TTest via "TTest 
as ITest".

A trait (in any of the proposed forms) would not allow that. (Hence a 
trait should probably also not be it the  TFoo=class(...) part).

So that makes 2 differences for trait vs interface
- The result of the composition can not be cast to either of the parts 
making it up
   That is a functional difference
- Traits remove the need to declare all methods twice (once in the 
interface, once in the class (or whatever implementing struct))
   That can be considered syntactic sugar (though it might have a quite 
impacting effect)


Further, IIRC it was said that:
For interfaces the syntax is already fully available. But some features 
are not yet supported by fpc.
Though Delphi has them, so the might come anyway.
However for traits as "default property" the syntax also already exists.



More information about the fpc-pascal mailing list