Skip to content

Commit 13e8ddb

Browse files
committed
Add some initial docs on multis
1 parent 08b6365 commit 13e8ddb

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

lib/Language/functions.pod

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,9 @@ But they can also be used on their own as anonymous blocks of code.
8181
For details about the syntax of blocks, see the documentation for the
8282
L<Block> type.
8383
84-
=head2 Multi-dispatch
85-
86-
TODO
87-
=comment multi, proto, {*}, only
88-
8984
=head2 Signatures
9085
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>.
9287
9388
=for code :allow<B>
9489
sub formatB<(Str $s)> { ... }
@@ -99,11 +94,39 @@ L<documentation on the C<Signature> class|Signature>.
9994
10095
=comment capture, destructuring,
10196
102-
=head2 Parameters
97+
=head2 Arguments
10398
10499
TODO
105100
=comment prefix:<|>, pair-chaining, \()
106101
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+
107130
=head1 Functions are First-Class Objects
108131
109132
TODO

0 commit comments

Comments
 (0)