routine does
| 1 | language documentation Type system |
| 1.1 | (Type system) trait does |
| 2 | language documentation Operators |
| 2.1 | (Operators) infix does |
| 3 | class Mu |
| 3.1 | (Mu) routine does |
Documentation for routine does assembled from the following types:
language documentation Type system
From Type system
(Type system) trait does
The trait does can be applied to roles and classes providing compile time mixins. To refer to a role that is not defined yet, use a forward declaration. The type name of the class with mixed in roles does not reflect the mixin, a type check does. If methods are provided in more than one mixed in role, the method that is defined first takes precedence. A list of roles separated by comma can be provided. In this case conflicts will be reported at compile time.
;does R2 ;;does R1 ;say [C ~~ R1, C ~~ R2];# OUTPUT: «[True True]»
For runtime mixins see but and does.
language documentation Operators
From Operators
(Operators) infix does
sub infix:<does>(Mu , Mu ) is assoc<non>
Mixes $role into $obj at runtime. Requires $obj to be mutable.
Similar to but operator, the $role can instead be an instantiated object, in which case, the operator will create a role for you automatically. The role will contain a single method named the same as $obj.^name and that returns $obj:
my = class .new;put ; # OUTPUT: «original»does "modded";put ; # OUTPUT: «modded»
If methods of the same name are present already, the last mixed in role takes precedence.
class Mu
From Mu
(Mu) routine does
method does(Mu --> Bool)
Returns True if and only if the invocant conforms to type $type.
my = Date.new('2016-06-03');say .does(Dateish); # True (Date does role Dateish)say .does(Any); # True (Date is a subclass of Any)say .does(DateTime); # False (Date is not a subclass of DateTime)
Unlike isa, which returns True only for superclasses, does includes both superclasses and roles.
say .isa(Dateish); # OUTPUT: «False»
Using the smartmatch operator ~~ is a more idiomatic alternative.
my = Date.new('2016-06-03');say ~~ Dateish; # OUTPUT: «True»say ~~ Any; # OUTPUT: «True»say ~~ DateTime; # OUTPUT: «False»