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 = C.new;
dd $c;
# OUTPUT: «C $c = C.new(a => "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{};
R[A.new].new;

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␤»