Documentation for routine does

Documentation for routine does, assembled from the following types:

language documentation Operators

From Operators

(Operators) infix does

sub infix:<does>(Mu $objMu $roleis assoc<non>

Mixes $role into $obj at run time. Requires $obj to be mutable.

$role doesn't need to a be a role, it can be something that knows how to act like a role; e.g., enum values.

If methods of the same name are present already, the last mixed in role takes precedence.

language documentation Typesystem

From Typesystem

(Typesystem) 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 then 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.

role R2 {...};
role R1 does R2 {};
role R2 {};
class C does R1 {};
say [C ~~ R1C ~~ R2];
# OUTPUT: «[True True]␤» 

For runtime mixins see but and does.

class Mu

From Mu

(Mu) routine does

method does(Mu $type --> Bool:D)

Returns True if and only if the invocant conforms to type $type.

my $d ='2016-06-03');
say $d.does(Dateish);             # True    (Date does role Dateish) 
say $d.does(Any);                 # True    (Date is a subclass of Any) 
say $d.does(DateTime);            # False   (Date is not a subclass of DateTime) 

Using the smart match operator ~~ is a more idiomatic alternative.

my $d ='2016-06-03');
say $d ~~ Dateish;                # OUTPUT: «True␤» 
say $d ~~ Any;                    # OUTPUT: «True␤» 
say $d ~~ DateTime;               # OUTPUT: «False␤»