Jump to page: 1 2
Thread overview
New concept Mixin Methods
Dec 25, 2022
Andre Pany
Dec 25, 2022
Timon Gehr
Dec 25, 2022
Paul Backus
Dec 25, 2022
Timon Gehr
Dec 27, 2022
Adam D Ruppe
Dec 28, 2022
Andre Pany
Dec 25, 2022
Hipreme
Dec 25, 2022
Andre Pany
Dec 25, 2022
Paul Backus
Dec 25, 2022
Paul Backus
Dec 28, 2022
WebFreak001
December 25, 2022

Hi,

Since a few days I think about a new concept called Mixin Methods.
I wonder whether it makes sense to write a DIP or I maybe miss an obvious issue with the concept?

Mixin Methods serves the purpose to enable introspection in class based frameworks by inheritance.

If you look at frameworks like d-unit (https://github.com/linkrope/dunit/blob/master/example.d) or arsd.jni (https://arsd-official.dpldocs.info/arsd.jni.html) they try to solve the issues in different ways.
In d-unit the unit test classes do not have inheritance but each unit test class needs to add a mixin statement mixin UnitTest;.

In arsd.jni there is another concept, the classes inherits introspection by inheriting from the root class and the current class as template parameter:

final class Hello : JavaClass!("", Hello) {
}

The issue with this concept is, all classes inherits from the root class (JavaClass) directly. Inheritance from another child class (e.g. Hello) is not possible.

The concept Mixin Methods can be used like this

import std;
void main()
{
   new Animal().fancyMethod();
   new Dog().fancyMethod();
   new Bulldog().fancyMethod();
}

class Animal {

    mixin void fancyMethod(){
        writeln(__traits(identifier, typeof(this)));
    }

}

class Dog : Animal {}

class Bulldog : Dog {}

Due to the mixin statement, the compiler will copy the fancyMethod into all sub classes of class Animal as method overload.

class Dog : Animal {
// compiler
override void fancyMethod(){
writeln(__traits(identifier, typeof(this)));
}
// compiler
}

class Bulldog : Dog {
// compiler
override void fancyMethod(){
writeln(__traits(identifier, typeof(this)));
}
// compiler
}

As you can see, Mixin Methods works for non static and non final methods. The benefit is, the users just need to inherit from a framework class to inherit the introspection capabilities.

Does it make sense to create here a DIP or do you see any road blocker?

Kind regards
Andre

December 25, 2022
On 12/25/22 12:07, Andre Pany wrote:
> 
> Does it make sense to create here a DIP or do you see any road blocker?

I have done this kind of thing manually in the past with mixin templates. The DMD frontend at some point turned unable to handle the combination of code generation, inheritance, recursion, and introspection.

So, go for it, but I am not so sure that there will be a working implementation.

Also, it would not be powerful enough for my use cases, it's sometimes useful to mixin some fields (or entire mixin templates) into child classes based on `typeof(this)`.
December 25, 2022

On Sunday, 25 December 2022 at 11:07:30 UTC, Andre Pany wrote:

>

The concept Mixin Methods can be used like this

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.

December 25, 2022
On 12/25/22 16:26, Paul Backus wrote:
> 
> This proposal fails on both counts: the problem it solves can already be solved by using a mixin template in the subclasses,

Don't try this at home. (It does not actually work.)

> 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.

I don't think it's about saving the typing, it's about not allowing bugs to appear because people _forgot_ to type.
December 25, 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

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).

December 25, 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

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

December 25, 2022

On Sunday, 25 December 2022 at 18:02:54 UTC, Andre Pany wrote:

>

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.

You may find some inspiration in Herb Sutter's proposal for metaclasses in C++:

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0707r4.pdf

December 26, 2022
On 26/12/2022 1:03 AM, Timon Gehr wrote:
> The DMD frontend at some point turned unable to handle the combination of code generation, inheritance, recursion, and introspection.

Yeah I've hit this in the past, its soured me pretty greatly on mixin templates unfortunately. I only use them sparingly now.

December 26, 2022
A long time ago I considered something similar, its a very neat idea.

Where its needed most is at the module level, for any imports get it mixed in. This covers pretty much anything rather than being specific to classes.

But there is always improving your build manager... Its been on my todo list for a while now (quite low), to refactor dub's generated file which lists all modules so that it is available for more than just unittest builds.
December 25, 2022
On Sunday, 25 December 2022 at 18:26:53 UTC, Richard (Rikki) Andrew Cattermole wrote:
> A long time ago I considered something similar, its a very neat idea.
>
> Where its needed most is at the module level, for any imports get it mixed in. This covers pretty much anything rather than being specific to classes.

You're supposed to tell horror stories on Halloween, not Christmas!

If you really want #include in D, you can use mixin(import("filename")). Personally, I would much prefer my modules to remain modular, please and thank you.
« First   ‹ Prev
1 2