Mixins

A mixin is a thing that mixes additional functionality into your existing objects. For that you need to have an object of some class and a role. A keyword does is used to make a mixin.

In the following example, an variable $var is an instance of the Int class, the one predefined in Perl 6. Then, a mixin is added to that object. The mixin behaviour is defined within the role Verbose. Object mutation happens at run-time.

my $var = Int.new();
$var = 42;
$var.say();

role Verbose {
    method say() {
        say "Value = " ~ self ~ '.';
    }
}
$var does Verbose;
$var.say();

Likewise with class hierarchy, mixin may hide methods already available in the class. Here, the say method was redefined and its default behaviour is no longer accessible for the variable's method with the same. Of course, new methods and class members may be creaeted with a mixin.

Mixines can be put on top of one another. In this case, the methods defined in the outmost ones win. Let's extend the previous example with another role layer:

role Verbose {
    method say() {
        say "Value = " ~ self ~ '.';
    }
}

role Silent {
    method say() {
        say "Secret value";
    }
}

my $var = 42;

$var does Verbose;
$var does Silent;

$var.say(); # Secret value

Now, roles Verbose and Silent subsequently mixed in the $var variable. Both roles contain its own say method, thus they replace the one existing for $var. That was the one available for Ints (although this time it was not explicitly stated that the object is of that type).

There is another method of mixing in more than one role: by listing them after a single does keyword. In this case, however, all the methods should have different name. Conflicting names will result in a compile error.

role Verbose {
    method show() {
        say "Value = " ~ self ~ '.';
    }
}

role Silent {
    method hide() {
        say "Secret value";
    }
}

my $var = 42;

$var does (Verbose, Silent);

$var.say();
$var.show();
$var.hide();

Here, the three methods, say, show and hide, coming from different classes and roles, were called on the object $var.