Named parameters: Difference between revisions

Add a Tcl example using dicts
(→‎{{header|ALGOL 68}}: Updated for latest ALGOL 68G, print an/a depending on whether the breed starts with a vowel or not)
(Add a Tcl example using dicts)
Line 44:
 
# due to the Yoneda ambiguity simple arguments must have an unique operator defined #
# E.g. a string cannot be coerced to a structure with a single string field #
OP NAME = (STRING name)OPTNAME: (OPTNAME opt; name OF opt := name; opt),
SPECIES = (STRING species)OPTSPECIES: (OPTSPECIES opt; species OF opt := species; opt),
BREED = (STRING breed)OPTBREED: (OPTBREED opt; breed OF opt := breed; opt);
 
PROC print pet = (OPTIONS option)VOID: (
STRING name:="Rex", species:="Dinosaur", breed:="Tyrannosaurus"; # Defaults #
Line 64:
,breed, " ", species, " named ",name," owned by ",owner, newline))
);
 
print pet((NAME "Mike", SPECIES "Dog", BREED "Irish Setter", OWNER("Harry", "S.", "Truman")));
print pet(()) # use print pet((EMPTY)) for Algol 68G version 2 #
Line 97:
<lang applescript>on getName(x) -- x assumed to be a record for this demo.
set x to x & {firstName:"?", lastName:"?"}
 
return x's firstName & ", " & x's lastName
end getName
Line 186:
{{out}}
 
<pre>argument x: 1
attribute foo: null
attribute bar: null
 
argument x: 2
attribute foo: foo
attribute bar: null
 
argument x: 3
attribute foo: null
attribute bar: bar
 
argument x: 4
attribute foo: 123
attribute bar: 124
 
argument x: 5
attribute foo: 123
attribute bar: 124</pre>
 
Line 214:
Static x=5, y=5, w=100, h=100, Count
Name:="AutoHotkey", Type:="Scripting", Text:="qwerty", Val:=True
 
Loop, Parse, _overrides,`,=, %A_Space% ; Override routine for Local/Static variables
A_Index & 1 ? (_:=A_LoopField) : (%_%:=A_LoopField)
 
Listvars
WinWaitClose, %A_ScriptFullPath%
Line 297:
FTest((FTest_args){ .y = 10, .z = 42 });
FT( .z = 47, .y = 10, .x = 42 );
 
// Default parameters
DFT();
DFT( .z = 99 );
 
// Default parameters with wrapper
DF2();
DF2( .z = 99 );
 
return 0;
}</lang>
Line 374:
int required_param_;
int optional_x_;
int optional_y_;
float optional_z_;
};</lang>
Line 389:
{{libheader|Boost}}
 
If you want real named parameters you can use The Boost Parameter Library.
<lang cpp>#include <boost/parameter/name.hpp>
#include <boost/parameter/preprocessor.hpp>
Line 404:
tag, // part of the deep magic. If you use BOOST_PARAMETER_NAME you need to put "tag" here.
(required // names and types of all required parameters, parentheses are required.
(foo, (int))
(bar, (float))
)
Line 410:
(baz, (bool) , false)
(bonk, (std::string), "default value")
)
)
{
Line 536:
 
writeln(sum(TParams._.z(4).x(3).y(5))); // 12
 
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
Line 633:
: is ( a "name" -- ) parse-name rot place ;
 
: greet ( -- )
cr ." Hello, " first-name count type space last-name count type ." !" ;
 
Line 641:
require mini-oof2.fs
require string.fs
object class
field: given-name
field: surname
end-class Person
 
: hiya ( -- )
cr ." Hiya, " given-name $. space surname $. ." !" ;
 
Line 779:
((variable(a[1:-2]) := get(A)) | # assign
runerr(205,a))) # . . problem
 
write(" x:=",x)
write(" y:=",y)
Line 807:
<lang j>NB. Strand notation
myFunc['c:\file.txt' 906 'blue' fs]
 
NB. Commas, like other langs
myFunc['c:\file.txt', 906, 'blue' fs]
 
NB. Unspecified args are defaulted ("optional")
myFunc['c:\file.txt' fs]
 
NB. Can use named arguments, like eg VB
myFunc[color='blue' fs]
 
NB. Often values needn't be quoted
myFunc[color= blue fs]
 
NB. Combination of comma syntax and name=value
myFunc[max=906, color=blue fs]
 
NB. Spelling of names is flexible
myFunc[MAX=906, COLOR=blue fs]
Line 828:
NB. Order of names is flexible
myFunc[COLOR=blue, MAX=906 fs]
 
NB. Even the delimiters are flexible...
myFunc<MAX=906, COLOR=blue fs></lang>
Line 875:
({ "name": "?"} + obj) as $obj # the default default value is null
| ($obj|.name) as $name
| ($obj|.first) as $first
| if ($first == null) then $name
else $name + ", " + $first
Line 882:
 
Here are examples of how the function can be invoked:
<lang jq>
formatName({"first": "George", "name": "Eliot"})
 
Line 898:
<lang Julia>function surround(string ; border = :default, padding = 0)
 
ve, ho, ul, ur, dl, dr =
border == :round ? ("\u2502","\u2500","\u256d","\u256e","\u2570","\u256f") :
border == :bold ? ("\u2503","\u2501","\u250F","\u2513","\u2517","\u251b") :
Line 904:
border == :dotted? ("\u254e","\u254c","\u250c","\u2510","\u2514","\u2518") :
border == :cross ? ("\u2502","\u2500","\u253c","\u253c","\u253c","\u253c") :
("\u2502","\u2500","\u250c","\u2510","\u2514","\u2518")
 
println(ul, ho^(length(string) + 2padding), ur, "\n",
Line 931:
fun main(args: Array<String>) {
// using positional parameters
someFunction("positional", 1, 2.0)
 
// using named parameters
Line 940:
 
// using first and third parameters in reverse
someFunction(third = 2.0, first = "reversed")
}</lang>
 
Line 1,063:
Named parameters are not natively supported. However, the following code can be used to implement them.
 
<lang Matlab> function foo(varargin)
for k= 1:2:length(varargin);
switch (varargin{k})
Line 1,070:
case {'param2'}
param2 = varargin{k+1};
end;
end;
printf('param1: %s\n',param1);
printf('param2: %s\n',param2);
end;
 
foo('param1','a1','param2','b2');
foo('param2','b2','param1','a1'); </lang>
 
Output:
<pre>>> foo('param1','a1','param2','b2');
param1: a1
param2: b2
>> foo('param2','b2','param1','a1');
param1: a1
param2: b2</pre>
Line 1,229:
my @known_params = qw(attendees event food time);
 
printf "%s called event() with the following named parameters:\n",
$name // 'Anonymous';
 
say sort map {
sprintf "%s: %s\n",
ucfirst $_,
ref $params{$_} eq ref []
? join ', ', @{ $params{$_} }
: $params{$_};
Line 1,249:
 
event(
{ # Curly braces with no label (e.g. 'sub' before it)
# create a reference to an anonymous hash
attendees => ['Bob', 'Betty', 'George', 'Bertha'],
Line 1,290:
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">fourdays</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- fourdays = timedelta(0,4) -- equivalent</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fourdays = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fourdays</span><span style="color: #0000FF;">)})</span>
 
<span style="color: #000080;font-style:italic;">-- **NB** a plain '=' is a very different thing:</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">wrong</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- equivalent to timedelta([weeks:=]iff((equal(days,4)?true:false))
-- - with an error if no local variable days exists.</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wrong = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneday</span><span style="color: #0000FF;">)})</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">hours</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">shift</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perfectly valid (param hours:=local hours)
-- timedelta(0,hours:=15,3) -- illegal (it is not clear whether you meant days:=3 or minutes:=3)</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"shift = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">shift</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
Line 1,436:
====Function Definition Parameters====
Function definitions in Python allow for the following ''parameter'' types:
* Optional ''default parameter'' types which are explicitly specified by name, and may have an optional default value.
* An optional ''positional parameter'' which is an identifier preceded by <code>"*"</code>.
* And an optional ''keyword parameter'' which is an identifier preceded by <code>"**"</code>.
Line 1,461:
* All positional arguments must appear before any keyword argument.
* ''Keyword arguments'' of the form <code>parameter_name "=" value</code> will map the value to the <code>defparameter</code> in the definition of the same name.
* ''Mapping arguments'' that are the characters <code>"**"</code> followed by an expression evaluating to a mapping (such as a dict/hash). The key, value pairs from the mapping are unpacked and mapped like individual keyword arguments to <code>defparameter</code>s of the function definition.
* If the function ''definition'' includes a ''positional parameter'', then if the assignment of ''positional arguments'' and ''sequence arguments'' in the ''call'' gives more values than the <code>defparameters</code> of the definition, then these extra arguments are assembled, in order, into a tuple that is assigned to the <code>posparameter</code> of the definition.
* If the function ''definition'' includes a ''keyword parameter'', then if the parameter name of any ''keyword arguments'' and ''mapping arguments'' in the ''call'' is unknown in the <code>defparameters</code> of the function definition, then these extra keyword/value pairs are assembled into a dict that is assigned to the <code>keyparameter</code> of the definition.
* Any ''default parameter'' of the function ''definition'' that is not assigned a value at this point, but which has a default value, will be aassigned this default value, without re-evaluating the default value.
* Any ''default parameter'' of the function ''definition'' that is still un-assigned will cause a <code>TypeError</code> exception to be raised.
* In addition, multiple mappings to any parameter will raise a <code>TypeError</code> exception. (This includes multiple mappings into a <code>keyparameter</code> or keyword arguments clashing with positional/sequence arguments).
Line 1,487:
====Examples====
<lang python>>>> from __future__ import print_function
>>>
>>> def show_args(defparam1, defparam2 = 'default value', *posparam, **keyparam):
"Straight-forward function to show its arguments"
Line 1,510:
print (" <None>")
 
 
>>> show_args('POSITIONAL', 'ARGUMENTS')
Default Parameters:
Line 1,551:
Keyword Arguments (by sorted key name):
<None>
>>> show_args('POSITIONAL', 'ARGUMENTS',
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
Default Parameters:
Line 1,562:
Keyword Arguments (by sorted key name):
<None>
>>> show_args('POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
Default Parameters:
Line 1,573:
keyword argument: kwa2 is: KEYWORD
keyword argument: kwa3 is: ARGUMENTS
>>> show_args('POSITIONAL',
'ARGUMENTS', 'EXTRA', 'POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
Default Parameters:
Line 1,588:
keyword argument: kwa3 is: ARGUMENTS
>>> # But note:
>>> show_args('POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS',
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
SyntaxError: non-keyword arg after keyword arg
Line 1,618:
(define (pizza sauce
;; mandatory keyword argument
#:topping topping
;; optional keyword argument with default
#:type [type "deep dish"])
Line 1,903:
"..."
initWithArray: anArray [ "single argument" ]
initWithArray: anArray andString: aString [
"two args; these two methods in usage resemble
a named argument, with optional andString argument"
Line 1,939:
test = function (one, two, three = '', four = '', five = '')
{
Print('one: ' $ one $ ', two: ' $ two $ ', three: ' $ three $
', four: ' $ four $ ', five: ' $ five)
}
Line 1,974:
 
=={{header|Tcl}}==
===Using arrays===
The simplest way of passing named parameters is to use the Tcl language's strong support for [[Varargs#Tcl|variadic commands]] together with its arrays. By convention (originally from [[Tk]]) the named parameters names start with a hyphen (“<tt>-</tt>”) and are called options.
<lang tcl>proc example args {
Line 2,008 ⟶ 2,009:
According to [http://wiki.tcl.tk/1730 wiki.tcl.tk discussions], '''::tcl::OptProc is deprecated.'''
The recommended replacement is [http://tcllib.sourceforge.net/doc/cmdline.html cmdline] in [http://tcllib.sourceforge.net/doc/index.html tcllib]. "This is probably the most standard and widely-used of these packages."
 
===Using dicts===
Now (8.5) that dicts are here to replace arrays in most places, this can be achieved more cleanly as long as you do the authorized key checking yourself. Example of summary testing (to put in a nice uplevel wrapper):
<lang tcl>proc example {x y args} {
set keyargs {arg1 default1 arg2 default2}
if {[llength $args] % 2 != 0} {
error "$args: invalid keyword arguments (spec: $keyargs)"
}
set margs [dict merge $keyargs $args]
if {[dict size $margs] != [dict size $keyargs]} {
error "$args: invalid keyword arguments (spec: $keyargs)"
}
lassign [dict values $margs] {*}[dict keys $margs]
puts "x: $x, y: $y, arg1: $arg1, arg2: $arg2"
}
example 1 2 # => x: 1, y: 2, arg1: default1, arg2: default2
example 1 2 arg2 3 # => x: 1, y: 2, arg1: default1, arg2: 3
example 1 2 test 3 # => test 3: invalid keyword arguments (spec: arg1 default1 arg2 default2)
example 1 2 test # => test: invalid keyword arguments (spec: arg1 default1 arg2 default2)</lang>
 
Of course, more work is required to reach the flexibility of something like Common Lisp's ordinary lambda list.
 
 
=={{header|VBA}}==
Anonymous user