Skip to content

Commit 7390cfe

Browse files
committed
Clarifies mixins examples
1 parent aede24b commit 7390cfe

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

doc/Language/operators.pod6

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,16 +1445,29 @@ the operator will create a role for you automatically. The role will contain
14451445
a single method named the same as C<$obj.^name> and that returns C<$obj>:
14461446
14471447
=begin code
1448-
say 42 but 'forty two'; # OUTPUT: «forty two␤»
1448+
my $forty-two = 42 but 'forty two';
1449+
say $forty-two+33; # OUTPUT: «75␤»
1450+
say $forty-two.^name; # OUTPUT: «Int+{<anon|1>}␤»
1451+
say $forty-two.Str; # OUTPUT: «forty two␤»
1452+
=end code
1453+
1454+
Calling C<^name> shows that the variable is an C<Int> with an anonymous object
1455+
mixed in. However, that object is of type C<Str>, so the variable, through the
1456+
mixin, is endowed with a method with that name, which is what we use in the last
1457+
sentence.
1458+
1459+
We can also mixin classes, even created on the fly.
14491460
1461+
=begin code
14501462
my $s = 12 but class Warbles { method hi { 'hello' } }.new;
14511463
say $s.Warbles.hi; # OUTPUT: «hello␤»
14521464
say $s + 42; # OUTPUT: «54␤»
14531465
=end code
14541466
1455-
If methods of the same name are present already, the last mixed in role takes
1456-
precedence. A list of methods can be provided in parentheses separated by
1457-
comma. In this case conflicts will be reported at runtime.
1467+
To access the mixed-in class, as above, we use the class name as is shown in the
1468+
second sentence. If methods of the same name are present already, the last mixed
1469+
in role takes precedence. A list of methods can be provided in parentheses
1470+
separated by comma. In this case conflicts will be reported at runtime.
14581471
14591472
=head2 infix C«cmp»
14601473

0 commit comments

Comments
 (0)