Named parameters: Difference between revisions

(→‎{{header|Lua}}: assignments were mis-matched)
Line 337:
 
Though this is cumbersome without using template Haskell, as the call site must supply the defaults.
 
=={{header|Icon}} and {{header|Unicon}}==
 
Icon and Unicon do not support named parameters. There are a couple of approaches that could be adapted to provide this kind of functionality. The basic considerations are:
* writing a string like "parm1=value" would be limiting as the value would have to be parsed and this form would be challenged to represent all data types
* using two parameters like this (...,"parm1:=",x,"parm2:=",y,...) removes this limitation
 
The test procedure below includes a list of valid parameter names to check against
 
 
<lang Icon>procedure main()
testproc("x:=",1,"y:=",2,"z:=",3)
testproc("x:=",3,"y:=",1,"z:=",2)
testproc("z:=",4,"x:=",2,"y:=",3)
testproc("i:=",1,"y:=",2,"z:=",3)
end
 
procedure testproc(A[]) #: demo to test named parameters
write("Calling testproc")
while a := get(A) do
(( a ? (v := =!["x","y","z"], =":=") | # valid parameter name?
stop("No parameter ",a)) &
((variable(a[1:-2]) := get(A)) | runerr(205,a))) # assign ?
write(" x:=",x)
write(" y:=",y)
write(" z:=",z)
end</lang>
{improve|Unicon|there is currently a bug in Icon/Unicon that causes assignments via variable("x") to fail for local and static variables. Remove this when this is fixed.}
 
Output:<pre>Calling testproc
x:=1
y:=2
z:=3
Calling testproc
x:=3
y:=1
z:=2
Calling testproc
x:=2
y:=3
z:=4
Calling testproc
No parameter i:=</pre>
 
=={{header|J}}==
Anonymous user