@@ -1445,16 +1445,29 @@ the operator will create a role for you automatically. The role will contain
1445
1445
a single method named the same as C < $obj.^name > and that returns C < $obj > :
1446
1446
1447
1447
= 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.
1449
1460
1461
+ = begin code
1450
1462
my $s = 12 but class Warbles { method hi { 'hello' } }.new;
1451
1463
say $s.Warbles.hi; # OUTPUT: «hello»
1452
1464
say $s + 42; # OUTPUT: «54»
1453
1465
= end code
1454
1466
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.
1458
1471
1459
1472
= head2 infix C « cmp »
1460
1473
0 commit comments