Doing some exploration involving creating and deleting namespaces, I ran into this curiosity:
% namespace eval foo {variable x 1}
% namespace upvar foo x x
% set x
1
% namespace delete foo
% set x
can't read "x": no such variable
% set x 2
can't set "x": upvar refers to variable in deleted namespace
%
After this, x
seems to be untouchable. It can't be read or unset because it doesn't exist, but it can't be set because it's in another namespace. How can x
be recovered from this state?
What I'm trying to accomplish is importing a set of commands and variables from another namespace, and being able to remove that namespace and have the imported commands and variables go away. namespace delete
seems to do the right thing with regards to imported commands, but there is no equivalent for imported variables, and as far as I can tell there isn't even any way to tell if a particular variable is imported (via namespace upvar
) from somewhere else - namespace which
tells you the current namespace, and namespace origin
doesn't exist for variables.
(not an answer)
Looks like a bug to me => https://core.tcl-lang.org/tcl/tktnew
You can't unset the variable either. It seems like the only thing you can do is upvar
it to a different variable.
% namespace eval foo {variable x 1}
% namespace upvar foo x x
% namespace delete foo
% set x
can't read "x": no such variable
% set x 1
can't set "x": upvar refers to variable in deleted namespace
% unset x
can't unset "x": no such variable
% set tmp ""
% upvar 0 tmp x
% unset x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With