Skip to content

Commit e835af6

Browse files
committed
Document **@ slurpies and "is raw", a few GLR tweaks, general cleanups
1 parent 173717a commit e835af6

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

lib/Type/Signature.pod

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
77
class Signature { ... }
88
9-
A signature is a static description of the parameter list of a code object.
10-
That is, it describes what and how many arguments you need to pass to
11-
the code or function in order to call it.
9+
A signature is a static description of the C<parameter|Parameter> list
10+
of a code object. That is, it describes what and how many arguments
11+
you need to pass to the code or function in order to call it.
1212
13-
Passing arguments to a signature I<binds> the arguments the parameters,
14-
and (loosely speaking) to the signature.
13+
Passing arguments to a signature I<binds> the arguments, contained in
14+
a L<Capture>, to the signature.
1515
1616
=head1 Signature Literals
1717
@@ -47,9 +47,10 @@ A signature consists of zero or more I<L<parameters|Parameter>>, separated by co
4747
sub add ($aB<,> $b) { $a + $b }
4848
4949
As an exception the first parameter may be followed by a colon instead
50-
of a comma to mark the invocant of a method (the invocant is that
51-
thing that's bound to L<C<self>>, but by specifying it in the
52-
signature, you can change what it's bound to).
50+
of a comma to mark the invocant of a method. The invocant is the
51+
thing that was used to call the method, which is usually bound to L<C<self>>
52+
By specifying it in the signature, you can change the variable name it
53+
is bound to.
5354
5455
=begin code :allow<B L>
5556
:(B<$a:> @b, %c) # first argument is the invocant
@@ -148,24 +149,25 @@ unnecessary. C<:(Num:_ $)> is the same as C<:(Num $)>.
148149
149150
=head2 X<Slurpy (A.K.A. Variadic) Parameters|parameter,*@;parameter,*%>
150151
151-
An array or hash parameter can be marked as I<slurpy> by a leading asterisk,
152+
An array or hash parameter can be marked as I<slurpy> by leading asterisk(s),
152153
which means it can bind to an arbitrary amount of arguments (zero or more).
153154
154155
These are called "slurpy" because they slurp up any remaining arguments
155156
to a function, like someone slurping up noodles.
156157
157158
=for code :allow<B L>
158-
:($a, @b) # exactly two arguments, where the second one must be Positional
159-
:($a, B<*@b>) # at least one argument, where @b is slurpy
159+
:($a, @b) # exactly two arguments, where the second one must be Positional
160+
:($a, B<*@b>) # at least one argument, @b slurps up any beyond that
160161
:(B<*%h>) # no positional arguments, but any number of named arguments
161162
162163
=for code :allow<B L>
163-
sub one-arg (@) { }
164-
sub slurpy (B<*@>) { }
165-
one-arg (5, 6, 7); # ok
164+
sub one-arg (@) { }
165+
sub slurpy (B<*@>) { }
166+
one-arg(5, 6, 7) ; # !!! too many arguments
167+
one-arg (5, 6, 7); # ok, same as one-arg((5,6,7))
166168
slurpy (5, 6, 7); # ok
167-
one-arg 5, 6, 7; # !!! too many arguments
168-
slurpy 5, 6, 7; # ok
169+
one-arg 5, 6, 7 ; # !!! too many arguments
170+
slurpy 5, 6, 7 ; # ok
169171
170172
=for code :allow<B L>
171173
sub named-names (B<*%named-args>) { %named-args.L<keys> }
@@ -176,6 +178,25 @@ parameters.
176178
177179
:(*@args, $last) # !!! Cannot put required parameter after variadic parameters
178180
181+
Slurpy parameters declared with one asterisk will flatten arguments by
182+
dissolving one layer of bare C<Iterables>. Slurpy parameters declared
183+
with two stars do not do so:
184+
185+
sub a (*@a) { @a.join("|").say };
186+
a(1,[1,2],[[3,4],5]); # 1|1|2|3 4|5
187+
sub b (**@b) { @b.join("|").say };
188+
b(1,[1,2],[[3,4],5]); # 1|1 2|3 4 5
189+
190+
Normally a slurpy parameter will create an Array, create a new
191+
Scalar container for each argument, and assign the value from each
192+
argument to those Scalars. If the original argument also had an
193+
intermediary Scalar it is bypassed during this process, and
194+
is not available inside the called function.
195+
196+
Slurpy parameters have special behaviors when combined with some
197+
L<traits and modifiers|Parameter Traits and Modifiers>,
198+
as described below.
199+
179200
=head2 Positional vs. Named
180201
181202
A parameter can be I<positional> or I<named>. All parameters are positional,
@@ -290,12 +311,23 @@ to be modified inside the routine
290311
291312
The C<is rw> trait makes the parameter only bind to a variable (or
292313
other writable container). Assigning to the parameter changes the
293-
value of the variable at the caller side
314+
value of the variable at the caller side.
294315
295316
sub swap($x is rw, $y is rw) {
296317
($x, $y) = ($y, $x);
297318
}
298319
320+
On slurpy parameters, C<is rw> is reserved for future use by language
321+
designers.
322+
323+
The C<is raw> trait is automatically applied to sigilless parameters,
324+
and may be used to make sigiled parameters behave like sigilless
325+
parameters. In the special case of slurpies, which normally produce
326+
an C<Array> full of C<Scalar>s as described above, C<is raw>
327+
will instead cause the parameter to produce a C<List>. Each element
328+
of that list will be bound directly as per a sigilless parameter,
329+
including any intermediary Scalars from the caller.
330+
299331
=head1 Methods
300332
301333
=head2 method params

0 commit comments

Comments
 (0)