$mixin

A mixin is a fragment of a class that is intended to be composed with other classes or mixins. The Xc++ language supports a specialised syntax for template mixins. The following presents some motivating examples and describes the syntax.

Mixins have been described in the literature as a remarkable means to achieve code reuse. See for example

A mixin is a capability that can be easily added to one or more classes. A mixin itself is never intended to be stand alone - i.e. to be instantiated in isolation. Rather it is only an adornment to be applied to some existing class.

Typically a mixin is highly reusable, and may be mixed into many different concrete classes. Therefore it can be regarded as a powerful means of achieving code reuse. There are different ways that the mixin concept has been implemented in C++. One way is to use multiple inheritance. However, there are many advantages to using single inheritance chains of mixins involving template classes that are parameterised on the base class.

According to this Java and C# don't support a general enough form of genericity to make the technique possible in those languages.

Mixins have both advantages and disadvantages.

Mixins provide an interesting approach to a Model-View-Controller (MVC) framework as described here.

Using Xcpp for mixins

The Xcpp front end provides specialised support for defining and using mixins.

Consider some mixins written in straight C++ used to write GUI widgets. Note that the long mixin chains are rather awkward syntactically.

The Xcpp front end provides a more convenient syntax. The same example can instead by written as follows


$mixin Rotate90
{
    int GetWidth() const
    {
        return BaseClass::GetHeight();
    }
    int GetHeight() const
    {
        return BaseClass::GetWidth();
    }
};
$mixin Scale<int scalex, int scaley>
{
    int GetWidth() const
    {
        return scalex * BaseClass::GetWidth();
    }
    int GetHeight() const
    {
        return scaley * BaseClass::GetHeight();
    }
};
$mixin Border<int border>
{
    int GetWidth() const
    {
        return 2*border + BaseClass::GetWidth();
    }
    int GetHeight() const
    {
        return 2*border + BaseClass::GetHeight();
    }
};
$mixin UnitSquare
{
    int GetWidth() const { return 1; }
    int GetHeight() const { return 1; }
};
$struct X
    mixin
    [
        UnitSquare
        Scale<2,1>
        Rotate90
        Border<1>
        Scale<10,3>
    ]
{
};

The mixin chain is enclosed in square brackets. It is interpreted as follows: we start with a unit square then apply Scale<2,1> to scale it horizontally. Next we rotate by 90 degrees, apply a border then finally scale it vertically and horizontally.

Special constructs within mixins

Within a $mixin the following have special meaning

Restrictions on $mixins

The following examples produce compile time errors:


$mixin X isa Y  // error : 'isa' prohibited on a $mixin
{
};

$mixin+ X       // error : '+' modifier not permitted on $mixin"
{
};