 |
GeneralStonewall Magician
Joined: 02 Feb 2004 Posts: 364 Location: USA
|
Posted: Fri Jun 18, 2010 7:34 am
[3.19f-] BUG\Suggestion: Named parameter conundrum. |
This is likely not new in 3.19x.
Take the following alias:
Code: |
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
<alias name="test" copy="yes">
<value>#if (%null($param)) {
$param = "was empty"
}
#print $param</value>
<arglist>$param</arglist>
</alias>
</cmud> |
The intent of the script is to check if $param is null and if it is then set it to some value. The issue is if no value is passed, then the local variable is not initialized until it's valued is set; But it's value is set inside the scope of the if statement, so as soon as the statement closes, $param is still uninitialized.
Here are some other ways that I tried; None of them work:
Code: |
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
<alias name="test2" copy="yes">
<value>$param = $param
#if (%null($param)) {
$param = "was empty"
}
#print $param</value>
<arglist>$param</arglist>
</alias>
<alias name="test3" copy="yes">
<value>#local $param
#if (%null($param)) {
$param = "was empty"
}
#print $param</value>
<arglist>$param</arglist>
</alias>
<alias name="test4" copy="yes">
<value>$param = %if( %null($param), "was empty", $param)
#print $param</value>
<arglist>$param</arglist>
</alias>
</cmud> |
So, basically it seems that named parameters need to be auto-initialized even if no arguments are passed. Default parameters would be nice, too. |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jun 18, 2010 4:51 pm |
Arguments to aliases are supposed to be "read only". For example, you cannot assign a value to %1, right? The Named arguments are just adding a name to the %1..%99 variables.
The issue is that internally, arguments to aliases and functions are passed by reference. So if you called your alias with something like:
test2 @myvar
and then tried to change the argument within the alias, you could easily end up changing the actual value of the passed @myvar variable. It probably wouldn't happen for normal variable values (strings, integers) but would likely to be a problem with json tables and COM values.
In any case, supporting writeable arguments is very tricky and not something that I can work on right now before the public version.
In your above script, use a real local variable to copy the value of $param and then manipulate the local variable and not the argument. |
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|