Skip to content

Commit 7fe076c

Browse files
committed
doc trait handles
1 parent b6d194d commit 7fe076c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/Language/typesystem.pod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,45 @@ TODO
1616
1717
TODO
1818
19+
=head3 X<C<handles>|handles trait> trait
20+
21+
Defined as:
22+
23+
multi sub trait_mod:<handles>(Attribute:D $target, $thunk)
24+
25+
The trait C<handles> applied to an attribute of a class will delegate all calls
26+
to the provided method name to the method with the same name of the attribute.
27+
The object referenced by the attribute must be initialized. A type constraint
28+
for the object that the call is delegated to can be provided.
29+
30+
class A { method m(){ 'A::m has been called.' } }
31+
class B is A { method m(){ 'B::m has been called.' } }
32+
class C {
33+
has A $.delegate handles 'm';
34+
method new($delegate){ self.bless(delegate => $delegate) }
35+
};
36+
say C.new(B.new).m(); # OUTPUT«B::m has been called.␤»
37+
38+
Instead of a method name a list of names, a C<Regex> or a C<Whatever> can be
39+
provided. In the latter case existing methods, both in the class itself and
40+
it's inheritance chain, will take precedence. If even local C<FALLBACK>s should
41+
be searched use a C<HyperWhatever>.
42+
43+
class A {
44+
method m1(){}
45+
method m2(){}
46+
}
47+
48+
class C {
49+
has $.delegate handles <m1 m2> = A.new()
50+
}
51+
C.new.m2;
52+
53+
class D {
54+
has $.delegate handles /m\d/ = A.new()
55+
}
56+
D.new.m1;
57+
1958
=head2 C<role>
2059
2160
TODO

0 commit comments

Comments
 (0)