Skip to content

Commit c4c4b53

Browse files
authored
In signatures.rakudoc, rearrange and modify the sections Sub-signatures, Captures, and Long names (#4646)
* In signatures.rakudoc, modify the sections Sub-signatures, Captures and Long names Move the entire section on Long names down, because Sub-signatures and Captures are very closely related to one another. In section Sub-signatures, change from using `|` to `\`, since captures with `|` are already the topic of the `Captures` section thereafter, and because `\` gives behavior that's more like that of an actual "compound parameter", e.g. that other parameters can still come behind them. Expand sections Sub-signatures and Captions by examples showing that most of the signature syntax is available for sub-signatures, too. In section Captures, move the paragraph on unbound captures to the bottom to get a good order of topics. * semicolons
1 parent 96c1086 commit c4c4b53

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

doc/Language/signatures.rakudoc

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,39 +1084,55 @@ X<|Language,sub-signature>
10841084
To match against a compound parameter use a sub-signature following the argument
10851085
name in parentheses.
10861086

1087-
sub foo(|c(Int, Str)){
1088-
put "called with {c.raku}"
1087+
sub foo(\p(Int, Str)){
1088+
put "called with {p.raku}"
10891089
};
1090-
foo(42, "answer");
1091-
# OUTPUT: «called with \(42, "answer")␤»
1090+
foo((42, "answer"));
1091+
# OUTPUT: «called with (42, "answer")␤»
10921092

1093-
=head1 X<Long names|Language,Long names>
1094-
1095-
To exclude certain parameters from being considered in multiple dispatch,
1096-
separate them with a double semicolon.
1093+
Sub-signatures can themselves use most of the syntax available for signatures.
10971094

1098-
multi f(Int $i, Str $s;; :$b) { say "$i, $s, {$b.raku}" };
1099-
f(10, 'answer');
1100-
# OUTPUT: «10, answer, Any␤»
1095+
sub bar(\p(Int $y where * > 5, Str $s?, *%h)) { put p.raku; put $s // "undefined"; }
1096+
bar((42, life => 40, universe => 41));
1097+
# OUTPUT: «(42, :life(40), :universe(41))␤undefined␤»
11011098

11021099
=head1 X<Capture parameters|Syntax,|>
11031100

11041101
Prefixing a parameter with a vertical bar C<|> makes the parameter a
11051102
L<C<Capture>|/type/Capture>, using up all the remaining positional and named
11061103
arguments.
11071104

1108-
This is often used in C<proto> definitions (like C<proto foo (|) {*}>) to
1105+
sub a(Int $i, |cap) { say $i; say cap.gist }
1106+
a(42, universe => 41, 1, 2, 3);
1107+
# OUTPUT: ›42␤\(1, 2, 3, :universe(41))␤»
1108+
1109+
Arguments captured to a variable can be forwarded as a whole using the slip
1110+
operator C<|>.
1111+
1112+
sub b(Int $i, Str $s) { say $i.^name ~ ' ' ~ $s.^name }
1113+
sub c(|cap) { say cap.^name; b(|cap) }
1114+
c(42, "answer");
1115+
# OUTPUT: «Capture␤Int Str␤»
1116+
1117+
One can also constrain the arguments subject to a C<Capture> by using a sub-signature.
1118+
1119+
sub d(|cap(Int, Str, *%)) { put "called with {cap.raku}" };
1120+
d(41);
1121+
# OUTPUT: «Too few positionals passed to 'd'; expected 2 arguments but got 1 in sub-signature or parameter cap␤ ...»
1122+
1123+
I<Unbound> C<Captures> are often used in C<proto> definitions (like C<proto foo (|) {*}>) to
11091124
indicate that the routine's L<C<multi> definitions|/syntax/multi> can have any L<type
11101125
constraints|#Type_constraints>. See L<proto|/language/functions#proto> for an
11111126
example.
11121127

1113-
If bound to a variable, arguments can be forwarded as a whole using the slip
1114-
operator C<|>.
1128+
=head1 X<Long names|Language,Long names>
11151129

1116-
sub a(Int $i, Str $s) { say $i.^name ~ ' ' ~ $s.^name }
1117-
sub b(|c) { say c.^name; a(|c) }
1118-
b(42, "answer");
1119-
# OUTPUT: «Capture␤Int Str␤»
1130+
To exclude certain parameters from being considered in multiple dispatch,
1131+
separate them with a double semicolon.
1132+
1133+
multi f(Int $i, Str $s;; :$b) { say "$i, $s, {$b.raku}" };
1134+
f(10, 'answer');
1135+
# OUTPUT: «10, answer, Any␤»
11201136

11211137
=head1 Parameter traits and modifiers
11221138

0 commit comments

Comments
 (0)