New concept Mixin Methods

Hipreme msnmancini at hotmail.com
Sun Dec 25 16:10:17 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.


So, this is a feature that seems like the `@:autoBuild` macro in 
Haxe does. Although it is only additive, I can find real nice use 
cases for that which is impossible in current D state.

So, this mixin methods would work for interfaces too? Another 
thing, I believe instead of of doing mixin methods, putting a way 
to do something like `class Test(mixin a!(), b!(), c!())` [All of 
those are mixin templates]. I believe that way is more readable, 
flexible and its syntax should be a lot easier to implement.



For instance, what is impossible to do is to create a registry in 
your classes when they extend a base class. For game engines, it 
is useful doing that as you would be able to track classes 
creation, including creating specific factories, you could 
implement UI based on that without requiring the user to actually 
put the `mixin template`. There is a case which I coded my game 
assets to be able to be multithreaded loaded by putting a mixin 
template, it would be a lot better if I could only just generate 
it automatically by both extending a class or by implementing an 
interface (aka simulating multi inheritance).




More information about the Digitalmars-d mailing list