6
6
7
7
class Signature { ... }
8
8
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.
12
12
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.
15
15
16
16
= head1 Signature Literals
17
17
@@ -47,9 +47,10 @@ A signature consists of zero or more I<L<parameters|Parameter>>, separated by co
47
47
sub add ($aB < , > $b) { $a + $b }
48
48
49
49
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.
53
54
54
55
= begin code :allow<B L>
55
56
:(B < $a: > @b, %c) # first argument is the invocant
@@ -148,24 +149,25 @@ unnecessary. C<:(Num:_ $)> is the same as C<:(Num $)>.
148
149
149
150
= head2 X < Slurpy (A.K.A. Variadic) Parameters|parameter,*@;parameter,*% >
150
151
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) ,
152
153
which means it can bind to an arbitrary amount of arguments (zero or more).
153
154
154
155
These are called "slurpy" because they slurp up any remaining arguments
155
156
to a function, like someone slurping up noodles.
156
157
157
158
= 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
160
161
:(B < *%h > ) # no positional arguments, but any number of named arguments
161
162
162
163
= 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))
166
168
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
169
171
170
172
= for code :allow<B L>
171
173
sub named-names (B < *%named-args > ) { %named-args.L < keys > }
@@ -176,6 +178,25 @@ parameters.
176
178
177
179
:(*@args, $last) # !!! Cannot put required parameter after variadic parameters
178
180
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
+
179
200
= head2 Positional vs. Named
180
201
181
202
A parameter can be I < positional > or I < named > . All parameters are positional,
@@ -290,12 +311,23 @@ to be modified inside the routine
290
311
291
312
The C < is rw > trait makes the parameter only bind to a variable (or
292
313
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.
294
315
295
316
sub swap($x is rw, $y is rw) {
296
317
($x, $y) = ($y, $x);
297
318
}
298
319
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
+
299
331
= head1 Methods
300
332
301
333
= head2 method params
0 commit comments