Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clarify use of | in proto, per #218
  • Loading branch information
Brock Wilcox committed Dec 4, 2015
1 parent 7c0d062 commit 0109b5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 13 additions & 6 deletions doc/Language/functions.pod
Expand Up @@ -184,16 +184,23 @@ C<proto> is a way to formally declare commonalities between C<multi>
candidates. It acts as a wrapper that can validate but not modify
arguments. Consider this basic example:
proto congratulate(Str $reason,Str $name,|) {*}
multi congratulate($reason,$name) {
proto congratulate(Str $reason, Str $name, |) {*}
multi congratulate($reason, $name) {
say "Hooray for your $reason, $name";
}
multi congratulate($reason, $name, Int $rank) {
say "Hooray for your $reason, $name -- you got rank $rank!";
}
congratulate('being a cool number',42);
congratulate('being a cool number', 'Fred'); # OK
congratulate('being a cool number', 'Fred', 42); # OK
congratulate('being a cool number', 42); # Proto match error
This fails at compile time because the proto's C<signature>
becomes the signature of the dispatcher and C<42> doesn't match
C<Str>.
All C<multi congratulate> will conform to the basic signature of two strings,
optionally followed by further parameters. The C<|> is an un-named C<Capture>
parameter, allowing a C<multi> which takes additional arguments. The third call
fails at compile time because the proto's C<signature> becomes the common
signature of all three, and C<42> doesn't match C<Str>.
say &congratulate.signature #-> (Str $reason, Str $name, | is raw)
Expand Down
7 changes: 4 additions & 3 deletions doc/Type/Signature.pod
Expand Up @@ -314,9 +314,10 @@ Prefixing a parameter with a vertical bar C<|> makes the parameter a
L<C<Capture>>, using up all the remaining positional and named
arguments.
This is often used in L<C<proto> definitions|proto> (like C<proto foo (|) {*}>)
to indicate that the routine's L<C<multi> definitions|multi> can have any
L<type constraints|#Type_Constraints>.
This is often used in C<proto> definitions (like C<proto foo (|) {*}>) to
indicate that the routine's L<C<multi> definitions|multi> can have any L<type
constraints|#Type_Constraints>. See L<proto|/language/functions#proto> for an
example.
=head2 X<Parameter Traits and Modifiers|trait,is copy;trait,is rw>
Expand Down

0 comments on commit 0109b5e

Please sign in to comment.