Conversation
Adding an additional type of named_parameters. While the regular named parameters (e.g. -parameter) are defined with the procedure definition e.g.
np::proc test {{a 1} {b 2} -- {c 3} } {
return [info vars *]
}
the new ad-hoc named parameters are defined at runtime
test -a 5 will have a=5, b=2, c=3
test -a 5 =d 4 will have a=5, b=2, c=3, d=4
of course any other suitable character could be chosen instead of '=' to identify these ad-hoc named characters, but the '=' is kind of charming.
Since this would be a new feature, the version number was bumped to 1.1.0.
added testcase 8.0 and 8.1, inspired by 7.0 and 7.1, to show the functionality for some "advanced" use cases
Adjusting testcases for a keyword based approach
- Bug fix, which allowed to set a variable via named paramater as well as keyword parameter
- changed adhoc named parameter to keyword parameters. Now parameters not defined as positional or or named parameters (e.g. =keyword parameter 1) will be collected in a dict kwargs. Via
dict for {key value} $kwargs {
puts "$key: $value"
}
one can cycle through the dict in the procedure body. In case no keyword argument was given, the dict will be empty.
Version number was changed to 1.1.1 as a suggestion.
Example:
np::proc test { {a 1} {b 2} {c 3} -- {ARGS ""}} {
puts "Variables known in proc:"
puts "+-NAMED------------------------------------------------------------"
puts "|"
puts "|a: $a"
puts "|b: $b"
puts "|c: $c"
puts "|"
puts "+-unNAMED----------------------------------------------------------"
puts "|"
puts "|ARGS: $ARGS"
puts "|"
puts "+-KWARGS-----------------------------------------------------------"
puts "|"
dict for {key value} $kwargs {
puts "|$key: $value"
}
puts "|"
puts "+------------------------------------------------------------------"
}
test -a 11 =d 44 -b -22 -- "filling up ARGS"
|
I changed the way those "adhoc named parameters" are made available within the procedure. Now they are collected within the dict kwargs. Therefore, a better name would be keyword arguments. will give as output. |
|
An additional comment: Of course the syntax with '=' identifying keyword parameters could be changed to a more open approach. The '=' could be ditched and whenever a named parameter (starting with '-') is encountered it will be handeled as before and in case it is not defined in the proc-head, it will be made available as keyword-parameter in the dict. This would result in a more consistenly looking syntax close to an approach as many might know from e.g. python. But this would give up the clear distinction between named parameters that are expected to be defined and those keyword parameters. If you were to get real crazy and push the door wide open, one could think about:
To be honest I do not really see why one should want this, but it would be a flexible option, handing power to the user. I personally would need an option to introduce named parameters in a readable way, without having to predefine them in the proc-head. This would be achieved by this pull request. If those keyword parameters are indicated via '-' or '=' is probably a matter of personal preference, as I can see points in favour of both implementations. I would slightly tend towards ditching the '=' in favour of using '-' for both. But that's obviously your call. Thanks for considering. |
Hi,
please find attached a suggestion for a syntax extension. This features adhoc named parameters (for a lack of a better name).
In my usecase it would be very desireable to be able to use the clean syntax of named parameters without the need to write a package with potentially thousands of autogenerated procedures with lots of named parameters, each given default values. I have the possibility to check in the body whether the adhoc named parameter(s) are sensible in the given context and can provide default values for the not given, but in the given context required additional parameters. This extension wouldn't break the existing functionality but would increase the flexibility.
Thanks for considering.