Defined[] with arrays Behavior change, intentional?
Posted: Sat Sep 16, 2017 9:55 pm
I didn't see this mentioned in any of the patch notes so I'm not sure if this was an intentional change or a side effect of the undeclared variable changes.
It deals with Defined[] and arrays.
Before the undeclared variables patch you could do something like this:
And it would echo TRUE. Now it echoes FALSE and you have to do this instead:
While the change is simple enough in an example like this, where it becomes a problem is if you are storing the variable name in a variable.
For a more specific example, I have a function that retrieves settings from an INI, automatically declares variables if the arguments passed tell it to, and things like that. So, as part of that, it passes in the variable name to store the INI setting into. When I'm loading arrays it would pass in the variable name as myArray[1] so that it does the /varset, it sets the value to myArray[1].
Example:
This of course isn't my exact code but it shows a use case. My ini load function is a bit more complicated because it can automatically create the setting in the INI if it doesn't exist, create it with a specified default, and things like that. And like I said, it also has the ability to declare variables that aren't defined, which is why with this patch I didn't have a problem in my macros with a bunch of undeclared variables (I did have problems with using ${Defined[]} on a lot of fucntion arguments that I had to make a ton of changes for but that is a different story). Something like this is really useful when loading multiple similar settings like a list of buffs and such.
Anyway, I got around it for now by defining another string and parsing out the array name using ${variableName.Left[${Math.Calc[${variableName.Find[[]} - 1]}]}. I'm just not sure if it's an unintended bug that someone might want to be aware of.
Also, this probably isn't the right place to ask this, but while I'm posting I'm also wondering if it would be possible since all these changes were made, to have an optional default value for function arguments. So that instead of everything defaulting to NULL if it's not defined, you can specify it's default value if it's not passed in.
Example:
It would be really nice, as that is the main problem I ran into with the recent change. I used to use Defined[] checks to declare my arguments locally if they weren't defined and set my default values. Now I have to check if they are == NULL if they are a number or boolean, or .Equal[NULL] if they are a string. I mean, it's fine I've made the changes at this point, but I still think it would be a good change to be able to define default values in the function definition.
It deals with Defined[] and arrays.
Before the undeclared variables patch you could do something like this:
Code: Select all
sub main
/declare myArray[20] string local NULL
/echo ${Defined[myArray[1]]}
/return
Code: Select all
sub main
/declare myArray[20] string local NULL
/echo ${Defined[myArray]}
/return
For a more specific example, I have a function that retrieves settings from an INI, automatically declares variables if the arguments passed tell it to, and things like that. So, as part of that, it passes in the variable name to store the INI setting into. When I'm loading arrays it would pass in the variable name as myArray[1] so that it does the /varset, it sets the value to myArray[1].
Example:
Code: Select all
sub main
/declare iniName string outer myini.ini
/declare myArray[20] string outer NULL
/for i 1 to 20
/call iniLoad General Setting${i} myArray[${i}]
/next i
/return
sub iniLoad(iniSection, iniKey, variableName)
/declare iniValue string local
/if (${Defined[${variableName}]}) {
/varset ${variableName} ${Ini[${iniName},${iniSection},${iniKey},NULL]}
}
/return
Anyway, I got around it for now by defining another string and parsing out the array name using ${variableName.Left[${Math.Calc[${variableName.Find[[]} - 1]}]}. I'm just not sure if it's an unintended bug that someone might want to be aware of.
Also, this probably isn't the right place to ask this, but while I'm posting I'm also wondering if it would be possible since all these changes were made, to have an optional default value for function arguments. So that instead of everything defaulting to NULL if it's not defined, you can specify it's default value if it's not passed in.
Example:
Code: Select all
sub myFunction( bool myBoolean TRUE, string myString SOME VALUE )
/return