The does routine

Combined from primary sources listed below.

In class Mu ( Type/Mu )§

See primary docmentation in context for routine does.

routine does§

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

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

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

Unlike isa, which returns True only for superclasses, does includes both superclasses and roles.

say $d.isa(Dateish); # OUTPUT: «False␤»

Using the smartmatch operator ~~ is a more idiomatic alternative.

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

In Type system ( Language/typesystem )§

See primary docmentation in context for trait does.

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.

role R2 {...};
role R1 does R2 {};
role R2 {};
class C does R1 {};

say [C ~~ R1, C ~~ R2];
# OUTPUT: «[True True]␤»

For runtime mixins see but and does.

In module Test ( Type/Test )§

See primary docmentation in context for sub does.

sub does-ok§

multi does-ok(Mu $var, Mu $type, $desc = "...")

Marks a test as passed if the given $var can do the given role $type. The function accepts an optional description of the test.

# create a Womble who can invent
role Invent {
    method brainstorm { say "Aha!" }
}
class Womble {}
class Tobermory is Womble does Invent {}

# ... and later in the tests
use Test;

my $tobermory = Tobermory.new;

# with automatically generated test description
does-ok $tobermory, Invent;
# => The object does role Type

does-ok $tobermory, Invent, "Tobermory can invent";
# => Tobermory can invent