Skip to content

Commit c7cf7c3

Browse files
committed
Improves documentation for shortcut, closes #2229
1 parent 463981f commit c7cf7c3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

doc/Type/Signature.pod6

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,10 @@ X<|positional argument>
643643
X<|named argument>
644644
=head2 Positional vs. named arguments
645645
646-
An argument can be I<positional> or I<named>. All arguments are positional,
647-
except slurpy hash and arguments marked with a leading colon C<:>.
648-
The latter is called a L<colon-pair|/type/Pair>.
646+
An argument can be I<positional> or I<named>. By default, arguments are
647+
positional, except slurpy hash and arguments marked with a leading colon C<:>.
648+
The latter is called a L<colon-pair|/type/Pair>. Check the following signatures
649+
and what they denote:
649650
650651
$ = :($a); # a positional argument
651652
$ = :(:$a); # a named argument of name 'a'
@@ -658,15 +659,26 @@ arguments are declared.
658659
sub pos($x, $y) { "x=$x y=$y" }
659660
pos(4, 5); # OUTPUT: «x=4 y=5»
660661
661-
In the case of named arguments and parameters, only the name is used for
662-
mapping arguments to parameters. If a fat arrow is used to construct a
663-
L<Pair|/type/Pair> only those with valid identifiers as keys are recognized
664-
as named arguments.
662+
In the case of named arguments and parameters, only the name is used for mapping
663+
arguments to parameters. If a fat arrow is used to construct a
664+
L<Pair|/type/Pair> only those with valid identifiers as keys are recognized as
665+
named arguments.
665666
666667
=for code :allow<L>
667668
sub named(:$x, :$y) { "x=$x y=$y" }
668669
named( y => 5, x => 4); # OUTPUT: «x=4 y=5»
669670
671+
You can invoke the routine using a variable with the same name as the named
672+
argument; in that case C<:> will be used for the invocation so that the name of
673+
the variable is understood as the key of the argument.
674+
675+
sub named-shortcut( :$shortcut ) {
676+
say "Looks like $shortcut"
677+
}
678+
named-shortcut( shortcut => "to here"); # OUTPUT: «Looks like to here␤»
679+
my $shortcut = "Þor is mighty";
680+
named-shortcut( :$shortcut ); # OUTPUT: «Looks like Þor is mighty␤»
681+
670682
It is possible to have a different name for a named argument than the
671683
variable name:
672684

0 commit comments

Comments
 (0)