@@ -81,14 +81,9 @@ But they can also be used on their own as anonymous blocks of code.
81
81
For details about the syntax of blocks, see the documentation for the
82
82
L < Block > type.
83
83
84
- = head2 Multi-dispatch
85
-
86
- TODO
87
- = comment multi, proto, {*}, only
88
-
89
84
= head2 Signatures
90
85
91
- The arguments that a function accepts are described in its I < signature > .
86
+ The parameters that a function accepts are described in its I < signature > .
92
87
93
88
= for code :allow<B>
94
89
sub formatB < (Str $s) > { ... }
@@ -99,11 +94,39 @@ L<documentation on the C<Signature> class|Signature>.
99
94
100
95
= comment capture, destructuring,
101
96
102
- = head2 Parameters
97
+ = head2 Arguments
103
98
104
99
TODO
105
100
= comment prefix:<|>, pair-chaining, \()
106
101
102
+ = head2 Multi-dispatch
103
+
104
+ Perl 6 allows you to write several routines with the same name, but different
105
+ signatures. When the routine is called by name, the runtime environment
106
+ decides which is the best match, and calls that I < candidate > .
107
+
108
+ multi congratulate($name) {
109
+ say "Happy birthday, $name";
110
+ }
111
+ multi congratulate($name, $age) {
112
+ say "Happy {$age}th birthday, $name";
113
+ }
114
+
115
+ congratulate 'Larry'; # Happy birthday, Larry
116
+ congratulate 'Bob', 45; # Happy 45th birthday, Bob
117
+
118
+ Dispatch can happen on the number of arguments (the I < arity > ), but also on the
119
+ type:
120
+
121
+ multi as-json(Bool $d) { $d ?? 'true' !! 'false'; }
122
+ multi as-json(Real $d) { ~$d }
123
+ multi as-json(@d) { sprintf '[%s]', @d.map(&as-json).join(', ') }
124
+
125
+ say as-json([True, 42]); # [true, 42]
126
+
127
+ = comment multi, proto, {*}, only
128
+
129
+
107
130
= head1 Functions are First-Class Objects
108
131
109
132
TODO
0 commit comments