@@ -419,8 +419,8 @@ Note that positional parameters aren't allowed after slurpy parameters.
419
419
# Cannot put required parameter $last after variadic parameters
420
420
= end code
421
421
422
- Normally a slurpy parameter will create an Array, create a new
423
- Scalar container for each argument, and assign the value from each
422
+ Normally a slurpy parameter will create an L < Array|/type/Array > , create a new
423
+ L < Scalar|/type/Scalar > container for each argument, and assign the value from each
424
424
argument to those Scalars. If the original argument also had an
425
425
intermediary Scalar it is bypassed during this process, and
426
426
is not available inside the called function.
@@ -446,54 +446,60 @@ each slurpy convention varies from the others.
446
446
= head3 Flattened Slurpy
447
447
448
448
Slurpy parameters declared with one asterisk will flatten arguments by
449
- dissolving one or more layers of bare C < Iterables > .
450
-
451
- my @array = <a b c>;
452
- my $list := <d e f>;
453
- sub a(*@a) { @a.perl.say };
454
- a(@array); # OUTPUT: «["a", "b", "c"]»
455
- a(1, $list, [2, 3]); # OUTPUT: «[1, "d", "e", "f", 2, 3]»
456
- a([1, 2]); # OUTPUT: «[1, 2]»
457
- a(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, 1, 2, 3, 4 5]»
458
- a(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
449
+ dissolving one or more layers of bare L < Iterable|/type/Iterable > s.
450
+
451
+ = begin code
452
+ my @array = <a b c>;
453
+ my $list := <d e f>;
454
+ sub a(*@a) { @a.perl.say };
455
+ a(@array); # OUTPUT: «["a", "b", "c"]»
456
+ a(1, $list, [2, 3]); # OUTPUT: «[1, "d", "e", "f", 2, 3]»
457
+ a([1, 2]); # OUTPUT: «[1, 2]»
458
+ a(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, 1, 2, 3, 4 5]»
459
+ a(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
460
+ = end code
459
461
460
462
A single asterisk slurpy flattens all given iterables, effectively hoisting any
461
463
object created with commas up to the top level.
462
464
463
465
= head3 Unflattened Slurpy
464
466
465
- Slurpy parameters declared with two stars do not flatten any iterable arguments
467
+ Slurpy parameters declared with two stars do not flatten any L < Iterable|/type/Iterable > arguments
466
468
within the list, but keep the arguments more or less as-is:
467
469
468
- my @array = <a b c>;
469
- my $list := <d e f>;
470
- sub b(**@b) { @b.perl.say };
471
- b(@array); # OUTPUT: «[["a", "b", "c"],]»
472
- b(1, $list, [2, 3]); # OUTPUT: «[1, ("d", "e", "f"), [2, 3]]»
473
- b([1, 2]); # OUTPUT: «[[1, 2],]»
474
- b(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, [1, 2], ([3, 4], 5)]»
475
- b(($_ for 1, 2, 3)); # OUTPUT: «[(1, 2, 3),]»
470
+ = begin code
471
+ my @array = <a b c>;
472
+ my $list := <d e f>;
473
+ sub b(**@b) { @b.perl.say };
474
+ b(@array); # OUTPUT: «[["a", "b", "c"],]»
475
+ b(1, $list, [2, 3]); # OUTPUT: «[1, ("d", "e", "f"), [2, 3]]»
476
+ b([1, 2]); # OUTPUT: «[[1, 2],]»
477
+ b(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, [1, 2], ([3, 4], 5)]»
478
+ b(($_ for 1, 2, 3)); # OUTPUT: «[(1, 2, 3),]»
479
+ = end code
476
480
477
481
The double asterisk slurpy hides the nested comma objects and leaves them as-is
478
482
in the slurpy array.
479
483
480
484
= head3 Single Argument Rule Slurpy
481
485
X < |+ (Single Argument Rule Slurpy) >
482
486
483
- A slurpy parameter created using a plus engages the "single argument rule",
487
+ A slurpy parameter created using a plus engages the I < "single argument rule" > ,
484
488
which decides how to handle the slurpy argument based upon context. Basically,
485
- if only a single argument is passed and that argument is iterable , that argument
489
+ if only a single argument is passed and that argument is L < Iterable|/type/Iterable > , that argument
486
490
is used to fill the slurpy parameter array. In any other case, C < +@ > works like
487
491
C < **@ > .
488
492
489
- my @array = <a b c>;
490
- my $list := <d e f>;
491
- sub c(+@b) { @b.perl.say };
492
- c(@array); # OUTPUT: «["a", "b", "c"]»
493
- c(1, $list, [2, 3]); # OUTPUT: «[1, ("d", "e", "f"), [2, 3]]»
494
- c([1, 2]); # OUTPUT: «[1, 2]»
495
- c(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, [1, 2], ([3, 4], 5)]»
496
- c(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
493
+ = begin code
494
+ my @array = <a b c>;
495
+ my $list := <d e f>;
496
+ sub c(+@b) { @b.perl.say };
497
+ c(@array); # OUTPUT: «["a", "b", "c"]»
498
+ c(1, $list, [2, 3]); # OUTPUT: «[1, ("d", "e", "f"), [2, 3]]»
499
+ c([1, 2]); # OUTPUT: «[1, 2]»
500
+ c(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, [1, 2], ([3, 4], 5)]»
501
+ c(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
502
+ = end code
497
503
498
504
For additional discussion and examples, see L < Slurpy Conventions for Functions|/language/functions#Slurpy_Conventions > .
499
505
0 commit comments