Documentation SETARRAY & SETLOCAL/ENDLOCAL

May 20, 2008
450
2
#1
It appears SETLOCAL/ENDLOCAL does not remove the SETARRAY created within it.
Code:
C:\>ver

TCC  16.01.45   Windows 7 [Version 6.1.7601]

C:\>echo %@arrayinfo[TestA]
-1

C:\>setlocal

C:\>setarray TestA[5]

C:\>endlocal

C:\>echo %@arrayinfo[TestA]
1

C:\>setarray TestA[5]
TCC: Array variable is already defined "TestA"
Since SET is used to both modify the environment and to modify array elements, I had expected the array to have been treated as part of the environment saved by SETLOCAL, which also includes aliases and functions. My preference is for the ENDLOCAL to remove any array variables created after the SETLOCAL. However, the documentation for the xARRAY and xLOCAL commands does not say one way or the other. What is supposed to happen? Do we need this check before every SETARRAY?
Code:
if %@arrayinfo[ArrayName] NE -1 unsetarray ArrayName
 
#2
Sorry, Tim, the 4th paragraph of HELP -> SETLOCAL is
SETLOCAL does not save the command history or array variables.
Ever since version 10... You are right that no similar indication is made in topic SETARRAY, but isn't its lack a hint?
BTW, while ALIAS mentions its relationship to SETLOCAL, there is no mention for FUNCTION.

If you feel strongly about extending the scope of SETLOCAL to SETARRAY, I'd support it in principle, but backward compatibility (for users who expect array definitions to survive ENDLOCAL) would be required. One possible method would be to use SETARRAY /L to define LOCAL arrays, i.e., arrays that must be defined within a SETLOCAL / ENDLOCAL pair. Another quirk is implied ENDLOCAL, i.e., a batch program with explicit SETLOCAL that performs ENDLOCAL on exit from the batch, whether or not present. I myself use implied ENDLOCAL whenever possible (laziness, probably not good programming practice).
 
May 20, 2008
450
2
#3
Thanks, Steve. I don't view not mentioning something as an indication of something else. I like the idea of the SETARRAY /L for local. All of my batch files use the implied ENDLOCAL also. Another variation of SETARRAY I would like is for it to redefine a previously defined array instead of triggering an error.