role Parameters

Roles can be provided with parameters in-between [] behind a roles name. Type captures are supported.

role R[$d{ has $.a = $d };
class C does R["default"{ };
my $c =;
dd $c;
# OUTPUT: «C $c = => "default")␤» 

Parameters can have type constraints, where clauses are not supported for types but can be implemented via subsets.

class A {};
class B {};
subset A-or-B where * ~~ A|B;
role R[A-or-B ::T{};

Default parameters can be provided.

role R[$p = fail("Please provide a parameter to role R")] {};
my $i = 1 does R;
CATCH { default { say .^name''.Str} }
# OUTPUT: «X::AdHoc: Could not instantiate role 'R':␤Please provide a parameter to role R␤»