[fpc-pascal] Traits Proposal

Michael Van Canneyt michael at freepascal.org
Wed Feb 10 16:41:18 CET 2021



On Wed, 10 Feb 2021, Ryan Joseph via fpc-pascal wrote:

>
>
>> On Feb 10, 2021, at 12:32 AM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>> 
>> Instead of trait you might as well use simply "object", so basically we
>> could simply state that instead of an interface, you can also specify an object
>> in the class definition:
>> 
>> 
>> TMyClass = class (TParent,TMyTrait1,TMyTrait2)
>> Public
>>  property Trait1: TMyTrait1 read FTrait1 implements TMyTrait1;
>>  property Trait2: TMyTrait2 read FTrait2 implements TMyTrait2;
>> end;
>> 
>
> Here's the old thread http://free-pascal-general.1045716.n5.nabble.com/Interface-delegates-and-the-implements-property-specifier-td5734729.html#a5734741. I think we agreed on introducing "trait" because it makes it clear that they are not objects and have other limitations. Under the hood they will be objects but it's important that the new type exists so the compiler can limit certain feature, like operator overloads and constructors (see below).

I heavily object to this; We have way too much keywords as it is already. 
So unless there really is no other way I don't think we should introduce
even more.

The compiler can do the check on an object for allowed/disallowed features
as soon as it is used in a 'trait'. So there really is no advantage gained 
by using 'trait'. The check just happens in a different place: not in the
object definition, but when it is used as a trait. At that point you must 
check for duplicate names (and probably other things) anyway.

Using 'object' has the additional advantage that you can simply use existing
objects, if they fulfill any restrictions, as traits.

If you impose the use of 'trait'  you risk that you must define objects twice 
to be able to use them as a trait - once standalone, once as a trait.

In Javascript you also don't need to use special classes/objects for mixins.

I really do not see a good reason to introduce a new type and matching
keyword.

Michael.


More information about the fpc-pascal mailing list