New concept Mixin Methods

Andre Pany andre at s-e-a-p.de
Sun Dec 25 18:02:54 UTC 2022


On Sunday, 25 December 2022 at 15:26:30 UTC, Paul Backus wrote:
> On Sunday, 25 December 2022 at 11:07:30 UTC, Andre Pany wrote:
>> The concept Mixin Methods can be used like this
>>
>> ```d
>> class Animal {
>>
>>     mixin void fancyMethod(){
>>         writeln(__traits(identifier, typeof(this)));
>>     }
>>
>> }
>> ```
>>
>> Due to the mixin statement, the compiler will copy the 
>> fancyMethod into all sub classes of class Animal as method 
>> overload.
>>
> [...]
>>
>> Does it make sense to create here a DIP or do you see any road 
>> blocker?
>
> I don't think this feature provides enough utility to justify a 
> DIP.
>
> Ideally, we should only add new features to the core language 
> when we cannot solve our problem any other way, and each 
> feature we add should be as powerful and general as possible, 
> so that we can solve a large number of problems with a small 
> number of features.
>
> This proposal fails on both counts: the problem it solves can 
> already be solved by using a mixin template in the subclasses, 
> so all it really does is save a bit of typing; and it only 
> saves that little bit of typing in this one, very specific 
> scenario.

Thanks all for the valuable feedback.

Yes, the essence of the idea is to make the usage of frameworks 
as simple as in other languages (e.g. Delphi, Java). Just by 
inheriting from a class, everything "just works".
In languages with runtime introspection capabilities this is 
easy. In D here is a small gap, in addition to inheritance you 
need to mixin the template in every class.

Yes, it is syntax sugar, but also avoids the issue that users 
forgets the mixin template statement.

Kind regards
Andre


More information about the Digitalmars-d mailing list