Skip to content

Commit a0cf51d

Browse files
committed
Add more words about duplicate key names and defaulting expression evaluation order
1 parent 7a4081f commit a0cf51d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ppcs/ppcTODO-signature-named-parameters.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ Each element provides a new lexical variable that is visible during the body of
4242

4343
The value of a named parameter is taken from the argument values passed by the caller, in a manner familiar to existing uses of hash assignment. The caller should pass an even-sized name-value pair list. The values corresponding to names of parameters will be assigned into the variables. The order in which the values are passed by the caller is not significant.
4444

45+
Since it is a relatively common pattern in callsites in existing code to rely on the semantics of assignment of name-value pair lists into hashes, the beahviour on encountering duplicate key names needs to be preserved. This is that duplicated key names do not raise an error or a warning, and simply accept the last value associated with that name. This allows callers to collect values from multiple sources with different orders of priority to override them; for example using a hash of values combined with individual elements:
46+
47+
```perl
48+
sub func ( :$abc, :$xyz, ... ) { ... }
49+
50+
func(
51+
abc => 123,
52+
%args,
53+
xyz => 789,
54+
);
55+
```
56+
57+
In this example, the given `abc` value will take effect unless overridden by a later value in `%args`, but the given `xyz` value will replace an earlier one given in `%args`. Neither situation will result in a warning or error.
58+
4559
Furthemore, all of the new behaviour is performed within the body of the invoked subroutine entirely by inspecting the values of the arguments that were passed. The subroutine is not made aware of how those values came to be passed in - whether from literal name-value syntax, a hash or array variable expansion, or any other expression yielding such a list of argument name and value pairs.
4660

4761
```perl
@@ -66,6 +80,8 @@ make_colour( red => 1.0, blue => 0.5 );
6680
# $blue = 0.5
6781
```
6882

83+
Since defaulting expressions are full expressions and not necessarily simple constant values, the time at which they are evaluated is significant. Much like with positional parameters, each is evaluated in order that it is written in source, left to right, and each can make use of values assigned by earlier expressions. This means they are evaluated in the order that is written in the function's declaration, which may not match the order that values were passed from the caller in the arguments list.
84+
6985
A subroutine is permitted to use a combination of *mandatory* positional and named parameters in its definition, provided that all named parameters appear after all the positional ones. Any named parameters may be optional; there are no ordering constraints here.
7086

7187
If a subroutine uses named parameters then it may optionally use a slurpy hash argument as its final element. In this case, the hash will receive all *other* name-value pairs passed by the caller, apart from those consumed by named parameters. If the subroutine does not use a slurpy argument, then it will be an error raised as an exception for there to be any remaining name-value pairs after processing.

0 commit comments

Comments
 (0)