Bindability checks

Traits can apply implicit constraints:

    multi swap($a is rw, $b is rw) {
        ($a, $b) = ($b, $a);
    }

This routine exchanges the contents of its two arguments. It must bind the two arguments as rw--both readable and writable. Calling the swap routine with an immutable value (for example a number literal) will fail.

The built-in function substr can not only extract parts of strings, but also modify them:

    # substr(String, Start, Length)
    say substr('Perl 5', 0, 4);         # prints B<Perl>

    my $p = 'Perl 5';
    # substr(String, Start, Length, Substitution)
    substr($p, 6, 1, '6');
    # now $p contains the string B<Perl 6>

You already know that the three-argument version and the four-argument version have different candidates: the latter binds its first argument as rw:

    multi substr($str, $start = 0, $length = *) { ... }
    multi substr($str is rw, $start, $length, $substitution) { ... }

This is also an example of candidates with different arity (number of expected arguments). This is seldom really necessary, because it is often a better alternative to make parameters optional. Cases where an arbitrary number of arguments are allowed are handled with slurpy parameters instead:

    sub mean(*@values) {
        ([+] @values) / @values;
    }