|
2024-09-10
| ||
| 06:38 | • Closed ticket [63806646d2]: Cannot create array in namespace if it already exist... artifact: 16f850fb3a user: oehhar | |
|
2024-09-09
| ||
| 15:47 | • New ticket [63806646d2]. artifact: b903551bca user: jal_frezie | |
Cannot create array in namespace if it already exists in base
Closed / Invalid63806646d2436e674070c90bd3e89e1cbc1c0eddCreated 2 years agoUpdated 2 years ago
Descriptionplain2024-09-09 15:47:05
If an array exists in the base namespace, then you add elements to it in a child namespace, they are added to the base namespace version only. Trying to read the array in the child namespace reads the base namespace version.
On the other hand if you create the array in the child namespace, then add elements to it in the base namespace, the two arrays remain distinct and each reads correctly in its own namespace.
Example:
$ tclsh
% info patchlevel
8.6.14
% array set foo {qaz 1 wsx 2 edc 3 rfv 4}
% namespace eval bar {array set foo {tgb 5 yhn 6 ujm 7}}
% array get foo
ujm 7 edc 3 qaz 1 tgb 5 rfv 4 wsx 2 yhn 6
% namespace eval bar {array get foo}
ujm 7 edc 3 qaz 1 tgb 5 rfv 4 wsx 2 yhn 6
% array get bar::foo
%
% namespace eval bar {array set xyzzy {tgb 5 yhn 6 ujm 7}}
% array get xyzzy
% namespace eval bar {array get xyzzy}
ujm 7 tgb 5 yhn 6
% array set xyzzy {qaz 1 wsx 2 edc 3 rfv 4}
% array get xyzzy
edc 3 qaz 1 rfv 4 wsx 2
% namespace eval bar {array get xyzzy}
ujm 7 tgb 5 yhn 6
% array get bar::xyzzy
ujm 7 tgb 5 yhn 6
%
oehharwiki2024-09-10 06:38:31
Yes, this is a known design choice in TCL 8.6, that global variables are seen in any namespace. This was changed in TCL 9.
Best see the migration document for an explanation.
And yes, it is weired and full of potential side effects...
THank you, Harald
Log in (anonymous is fine) to add a comment.