Basically a mixin class is macro or template that provides an implementation that can be inherited, but cannot be instanciated by itself. A class can inherit from multiple mixins, which fulfills the same purpose as multiple inheritance in C++, i.e. code reuse, but don't add the complexities of multiple base classes to the runtime, i.e. multiple distinct virtual function tables, and object pointers that change depending on which base class that is represented.
When inheriting from a mixin, the class will receive the properties and methods from the mixin that doesn't conflict already existing properties and methods in the class (e.g. explicitly declared, or inherited from another class/mixin). The mixin class methods are compiled in the context of the derived class, so they will see and be able to use properties declared in the derived class even if they are not declared in the mixin class itself. This also allows the derived class to override the type of a property declared in a mixin (assuming the code that accesses the property is compatible).
// Declare a mixin class
mixin class MyMixin1
{
void increase() { property++; }
int property;
}
mixin class MyMixin2
{
void decrease() { property--; }
void setName(string name) { this.name = name; }
int property;
}
// Declare a class that inherits from a mixin
class MyClass : MyMixin1, MyMixin2
{
// Will receive property and increase() from MyMixin1
// Will receive decrease and setName() from MyMixin2
string name;
}
MyMixin1 a; // Not OK. A mixin class cannot be instanciated
MyClass b;
void main()
{
MyMixin2 @c = cast<MyMixin2>(b); // Not OK. A mixin class isn't a real type
}
A mixin class will also be able to implement interfaces, and perhaps even other classes. When a class inherits from a mixin like that the derived class will also implement the interfaces, just as if it had implemented them explicitly. (This part will probably not be implemented in the first iteration, planned for version 2.25.0).
To begin with a mixin class will not be able to implement constructors and/or destructors. If a specific initialization/uninitialization should be done for properties in the mixin class, the implementation of that has to be done with ordinary methods that are explicitly called by the class that inherits the mixin class. It would be better if the constructor/destructor could be used, but I haven't quite figured out how to merge the implementation into the final constructor/destructor that will be built for the class that inherits the mixin.
Let me know what you think of this, and if you have any suggestions for modifications or further additions to make the feature even better.
Regards.
Andreas