Named parameters: Difference between revisions

Content added Content deleted
(→‎{{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: Line 44:


# due to the Yoneda ambiguity simple arguments must have an unique operator defined #
# 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 #
# 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),
OP NAME = (STRING name)OPTNAME: (OPTNAME opt; name OF opt := name; opt),
SPECIES = (STRING species)OPTSPECIES: (OPTSPECIES opt; species OF opt := species; opt),
SPECIES = (STRING species)OPTSPECIES: (OPTSPECIES opt; species OF opt := species; opt),
BREED = (STRING breed)OPTBREED: (OPTBREED opt; breed OF opt := breed; opt);
BREED = (STRING breed)OPTBREED: (OPTBREED opt; breed OF opt := breed; opt);

PROC print pet = (OPTIONS option)VOID: (
PROC print pet = (OPTIONS option)VOID: (
STRING name:="Rex", species:="Dinosaur", breed:="Tyrannosaurus"; # Defaults #
STRING name:="Rex", species:="Dinosaur", breed:="Tyrannosaurus"; # Defaults #
Line 64: Line 64:
,breed, " ", species, " named ",name," owned by ",owner, newline))
,breed, " ", species, " named ",name," owned by ",owner, newline))
);
);

print pet((NAME "Mike", SPECIES "Dog", BREED "Irish Setter", OWNER("Harry", "S.", "Truman")));
print pet((NAME "Mike", SPECIES "Dog", BREED "Irish Setter", OWNER("Harry", "S.", "Truman")));
print pet(()) # use print pet((EMPTY)) for Algol 68G version 2 #
print pet(()) # use print pet((EMPTY)) for Algol 68G version 2 #
Line 97: Line 97:
<lang applescript>on getName(x) -- x assumed to be a record for this demo.
<lang applescript>on getName(x) -- x assumed to be a record for this demo.
set x to x & {firstName:"?", lastName:"?"}
set x to x & {firstName:"?", lastName:"?"}

return x's firstName & ", " & x's lastName
return x's firstName & ", " & x's lastName
end getName
end getName
Line 186: Line 186:
{{out}}
{{out}}


<pre>argument x: 1
<pre>argument x: 1
attribute foo: null
attribute foo: null
attribute bar: null
attribute bar: null


argument x: 2
argument x: 2
attribute foo: foo
attribute foo: foo
attribute bar: null
attribute bar: null


argument x: 3
argument x: 3
attribute foo: null
attribute foo: null
attribute bar: bar
attribute bar: bar


argument x: 4
argument x: 4
attribute foo: 123
attribute foo: 123
attribute bar: 124
attribute bar: 124


argument x: 5
argument x: 5
attribute foo: 123
attribute foo: 123
attribute bar: 124</pre>
attribute bar: 124</pre>


Line 214: Line 214:
Static x=5, y=5, w=100, h=100, Count
Static x=5, y=5, w=100, h=100, Count
Name:="AutoHotkey", Type:="Scripting", Text:="qwerty", Val:=True
Name:="AutoHotkey", Type:="Scripting", Text:="qwerty", Val:=True

Loop, Parse, _overrides,`,=, %A_Space% ; Override routine for Local/Static variables
Loop, Parse, _overrides,`,=, %A_Space% ; Override routine for Local/Static variables
A_Index & 1 ? (_:=A_LoopField) : (%_%:=A_LoopField)
A_Index & 1 ? (_:=A_LoopField) : (%_%:=A_LoopField)

Listvars
Listvars
WinWaitClose, %A_ScriptFullPath%
WinWaitClose, %A_ScriptFullPath%
Line 297: Line 297:
FTest((FTest_args){ .y = 10, .z = 42 });
FTest((FTest_args){ .y = 10, .z = 42 });
FT( .z = 47, .y = 10, .x = 42 );
FT( .z = 47, .y = 10, .x = 42 );

// Default parameters
// Default parameters
DFT();
DFT();
DFT( .z = 99 );
DFT( .z = 99 );

// Default parameters with wrapper
// Default parameters with wrapper
DF2();
DF2();
DF2( .z = 99 );
DF2( .z = 99 );

return 0;
return 0;
}</lang>
}</lang>
Line 374: Line 374:
int required_param_;
int required_param_;
int optional_x_;
int optional_x_;
int optional_y_;
int optional_y_;
float optional_z_;
float optional_z_;
};</lang>
};</lang>
Line 389: Line 389:
{{libheader|Boost}}
{{libheader|Boost}}


If you want real named parameters you can use The Boost Parameter Library.
If you want real named parameters you can use The Boost Parameter Library.
<lang cpp>#include <boost/parameter/name.hpp>
<lang cpp>#include <boost/parameter/name.hpp>
#include <boost/parameter/preprocessor.hpp>
#include <boost/parameter/preprocessor.hpp>
Line 404: Line 404:
tag, // part of the deep magic. If you use BOOST_PARAMETER_NAME you need to put "tag" here.
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.
(required // names and types of all required parameters, parentheses are required.
(foo, (int))
(foo, (int))
(bar, (float))
(bar, (float))
)
)
Line 410: Line 410:
(baz, (bool) , false)
(baz, (bool) , false)
(bonk, (std::string), "default value")
(bonk, (std::string), "default value")
)
)
)
)
{
{
Line 536: Line 536:


writeln(sum(TParams._.z(4).x(3).y(5))); // 12
writeln(sum(TParams._.z(4).x(3).y(5))); // 12

{$IFNDEF UNIX} readln; {$ENDIF}
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
end.</lang>
Line 633: Line 633:
: is ( a "name" -- ) parse-name rot place ;
: is ( a "name" -- ) parse-name rot place ;


: greet ( -- )
: greet ( -- )
cr ." Hello, " first-name count type space last-name count type ." !" ;
cr ." Hello, " first-name count type space last-name count type ." !" ;


Line 641: Line 641:
require mini-oof2.fs
require mini-oof2.fs
require string.fs
require string.fs
object class
object class
field: given-name
field: given-name
field: surname
field: surname
end-class Person
end-class Person


: hiya ( -- )
: hiya ( -- )
cr ." Hiya, " given-name $. space surname $. ." !" ;
cr ." Hiya, " given-name $. space surname $. ." !" ;


Line 779: Line 779:
((variable(a[1:-2]) := get(A)) | # assign
((variable(a[1:-2]) := get(A)) | # assign
runerr(205,a))) # . . problem
runerr(205,a))) # . . problem

write(" x:=",x)
write(" x:=",x)
write(" y:=",y)
write(" y:=",y)
Line 807: Line 807:
<lang j>NB. Strand notation
<lang j>NB. Strand notation
myFunc['c:\file.txt' 906 'blue' fs]
myFunc['c:\file.txt' 906 'blue' fs]

NB. Commas, like other langs
NB. Commas, like other langs
myFunc['c:\file.txt', 906, 'blue' fs]
myFunc['c:\file.txt', 906, 'blue' fs]

NB. Unspecified args are defaulted ("optional")
NB. Unspecified args are defaulted ("optional")
myFunc['c:\file.txt' fs]
myFunc['c:\file.txt' fs]

NB. Can use named arguments, like eg VB
NB. Can use named arguments, like eg VB
myFunc[color='blue' fs]
myFunc[color='blue' fs]

NB. Often values needn't be quoted
NB. Often values needn't be quoted
myFunc[color= blue fs]
myFunc[color= blue fs]

NB. Combination of comma syntax and name=value
NB. Combination of comma syntax and name=value
myFunc[max=906, color=blue fs]
myFunc[max=906, color=blue fs]

NB. Spelling of names is flexible
NB. Spelling of names is flexible
myFunc[MAX=906, COLOR=blue fs]
myFunc[MAX=906, COLOR=blue fs]
Line 828: Line 828:
NB. Order of names is flexible
NB. Order of names is flexible
myFunc[COLOR=blue, MAX=906 fs]
myFunc[COLOR=blue, MAX=906 fs]

NB. Even the delimiters are flexible...
NB. Even the delimiters are flexible...
myFunc<MAX=906, COLOR=blue fs></lang>
myFunc<MAX=906, COLOR=blue fs></lang>
Line 875: Line 875:
({ "name": "?"} + obj) as $obj # the default default value is null
({ "name": "?"} + obj) as $obj # the default default value is null
| ($obj|.name) as $name
| ($obj|.name) as $name
| ($obj|.first) as $first
| ($obj|.first) as $first
| if ($first == null) then $name
| if ($first == null) then $name
else $name + ", " + $first
else $name + ", " + $first
Line 882: Line 882:


Here are examples of how the function can be invoked:
Here are examples of how the function can be invoked:
<lang jq>
<lang jq>
formatName({"first": "George", "name": "Eliot"})
formatName({"first": "George", "name": "Eliot"})


Line 898: Line 898:
<lang Julia>function surround(string ; border = :default, padding = 0)
<lang Julia>function surround(string ; border = :default, padding = 0)


ve, ho, ul, ur, dl, dr =
ve, ho, ul, ur, dl, dr =
border == :round ? ("\u2502","\u2500","\u256d","\u256e","\u2570","\u256f") :
border == :round ? ("\u2502","\u2500","\u256d","\u256e","\u2570","\u256f") :
border == :bold ? ("\u2503","\u2501","\u250F","\u2513","\u2517","\u251b") :
border == :bold ? ("\u2503","\u2501","\u250F","\u2513","\u2517","\u251b") :
Line 904: Line 904:
border == :dotted? ("\u254e","\u254c","\u250c","\u2510","\u2514","\u2518") :
border == :dotted? ("\u254e","\u254c","\u250c","\u2510","\u2514","\u2518") :
border == :cross ? ("\u2502","\u2500","\u253c","\u253c","\u253c","\u253c") :
border == :cross ? ("\u2502","\u2500","\u253c","\u253c","\u253c","\u253c") :
("\u2502","\u2500","\u250c","\u2510","\u2514","\u2518")
("\u2502","\u2500","\u250c","\u2510","\u2514","\u2518")


println(ul, ho^(length(string) + 2padding), ur, "\n",
println(ul, ho^(length(string) + 2padding), ur, "\n",
Line 931: Line 931:
fun main(args: Array<String>) {
fun main(args: Array<String>) {
// using positional parameters
// using positional parameters
someFunction("positional", 1, 2.0)
someFunction("positional", 1, 2.0)


// using named parameters
// using named parameters
Line 940: Line 940:


// using first and third parameters in reverse
// using first and third parameters in reverse
someFunction(third = 2.0, first = "reversed")
someFunction(third = 2.0, first = "reversed")
}</lang>
}</lang>


Line 1,063: Line 1,063:
Named parameters are not natively supported. However, the following code can be used to implement them.
Named parameters are not natively supported. However, the following code can be used to implement them.


<lang Matlab> function foo(varargin)
<lang Matlab> function foo(varargin)
for k= 1:2:length(varargin);
for k= 1:2:length(varargin);
switch (varargin{k})
switch (varargin{k})
Line 1,070: Line 1,070:
case {'param2'}
case {'param2'}
param2 = varargin{k+1};
param2 = varargin{k+1};
end;
end;
end;
end;
printf('param1: %s\n',param1);
printf('param1: %s\n',param1);
printf('param2: %s\n',param2);
printf('param2: %s\n',param2);
end;
end;

foo('param1','a1','param2','b2');
foo('param1','a1','param2','b2');
foo('param2','b2','param1','a1'); </lang>
foo('param2','b2','param1','a1'); </lang>


Output:
Output:
<pre>>> foo('param1','a1','param2','b2');
<pre>>> foo('param1','a1','param2','b2');
param1: a1
param1: a1
param2: b2
param2: b2
>> foo('param2','b2','param1','a1');
>> foo('param2','b2','param1','a1');
param1: a1
param1: a1
param2: b2</pre>
param2: b2</pre>
Line 1,229: Line 1,229:
my @known_params = qw(attendees event food time);
my @known_params = qw(attendees event food time);


printf "%s called event() with the following named parameters:\n",
printf "%s called event() with the following named parameters:\n",
$name // 'Anonymous';
$name // 'Anonymous';


say sort map {
say sort map {
sprintf "%s: %s\n",
sprintf "%s: %s\n",
ucfirst $_,
ucfirst $_,
ref $params{$_} eq ref []
ref $params{$_} eq ref []
? join ', ', @{ $params{$_} }
? join ', ', @{ $params{$_} }
: $params{$_};
: $params{$_};
Line 1,249: Line 1,249:


event(
event(
{ # Curly braces with no label (e.g. 'sub' before it)
{ # Curly braces with no label (e.g. 'sub' before it)
# create a reference to an anonymous hash
# create a reference to an anonymous hash
attendees => ['Bob', 'Betty', 'George', 'Bertha'],
attendees => ['Bob', 'Betty', 'George', 'Bertha'],
Line 1,290: Line 1,290:
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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;">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: #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: #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: #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: #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: #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))
<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>
-- - 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: #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: #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)
<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>
-- 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>
<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>-->
<!--</lang>-->
Line 1,436: Line 1,436:
====Function Definition Parameters====
====Function Definition Parameters====
Function definitions in Python allow for the following ''parameter'' types:
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.
* 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>.
* 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>.
* And an optional ''keyword parameter'' which is an identifier preceded by <code>"**"</code>.
Line 1,461: Line 1,461:
* All positional arguments must appear before any keyword argument.
* 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.
* ''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.
* ''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 ''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.
* 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 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.
* 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).
* 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: Line 1,487:
====Examples====
====Examples====
<lang python>>>> from __future__ import print_function
<lang python>>>> from __future__ import print_function
>>>
>>>
>>> def show_args(defparam1, defparam2 = 'default value', *posparam, **keyparam):
>>> def show_args(defparam1, defparam2 = 'default value', *posparam, **keyparam):
"Straight-forward function to show its arguments"
"Straight-forward function to show its arguments"
Line 1,510: Line 1,510:
print (" <None>")
print (" <None>")



>>> show_args('POSITIONAL', 'ARGUMENTS')
>>> show_args('POSITIONAL', 'ARGUMENTS')
Default Parameters:
Default Parameters:
Line 1,551: Line 1,551:
Keyword Arguments (by sorted key name):
Keyword Arguments (by sorted key name):
<None>
<None>
>>> show_args('POSITIONAL', 'ARGUMENTS',
>>> show_args('POSITIONAL', 'ARGUMENTS',
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
Default Parameters:
Default Parameters:
Line 1,562: Line 1,562:
Keyword Arguments (by sorted key name):
Keyword Arguments (by sorted key name):
<None>
<None>
>>> show_args('POSITIONAL', 'ARGUMENTS',
>>> show_args('POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
Default Parameters:
Default Parameters:
Line 1,573: Line 1,573:
keyword argument: kwa2 is: KEYWORD
keyword argument: kwa2 is: KEYWORD
keyword argument: kwa3 is: ARGUMENTS
keyword argument: kwa3 is: ARGUMENTS
>>> show_args('POSITIONAL',
>>> show_args('POSITIONAL',
'ARGUMENTS', 'EXTRA', 'POSITIONAL', 'ARGUMENTS',
'ARGUMENTS', 'EXTRA', 'POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
Default Parameters:
Default Parameters:
Line 1,588: Line 1,588:
keyword argument: kwa3 is: ARGUMENTS
keyword argument: kwa3 is: ARGUMENTS
>>> # But note:
>>> # But note:
>>> show_args('POSITIONAL', 'ARGUMENTS',
>>> show_args('POSITIONAL', 'ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS',
kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS',
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
'EXTRA', 'POSITIONAL', 'ARGUMENTS')
SyntaxError: non-keyword arg after keyword arg
SyntaxError: non-keyword arg after keyword arg
Line 1,618: Line 1,618:
(define (pizza sauce
(define (pizza sauce
;; mandatory keyword argument
;; mandatory keyword argument
#:topping topping
#:topping topping
;; optional keyword argument with default
;; optional keyword argument with default
#:type [type "deep dish"])
#:type [type "deep dish"])
Line 1,903: Line 1,903:
"..."
"..."
initWithArray: anArray [ "single argument" ]
initWithArray: anArray [ "single argument" ]
initWithArray: anArray andString: aString [
initWithArray: anArray andString: aString [
"two args; these two methods in usage resemble
"two args; these two methods in usage resemble
a named argument, with optional andString argument"
a named argument, with optional andString argument"
Line 1,939: Line 1,939:
test = function (one, two, three = '', four = '', five = '')
test = function (one, two, three = '', four = '', five = '')
{
{
Print('one: ' $ one $ ', two: ' $ two $ ', three: ' $ three $
Print('one: ' $ one $ ', two: ' $ two $ ', three: ' $ three $
', four: ' $ four $ ', five: ' $ five)
', four: ' $ four $ ', five: ' $ five)
}
}
Line 1,974: Line 1,974:


=={{header|Tcl}}==
=={{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.
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 {
<lang tcl>proc example args {
Line 2,008: Line 2,009:
According to [http://wiki.tcl.tk/1730 wiki.tcl.tk discussions], '''::tcl::OptProc is deprecated.'''
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."
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}}==
=={{header|VBA}}==