On 2023-06-11, Helmut Giese wrote:
sorry if this is something obvious,
but I don't know if I can trust my eyes. I have been sweating
several hours over the "problem" to get a command executed inside
a namespace - only to find out, that the cause seems to be
something totally different.
(NEW2) 137 % namespace eval sim {}
(NEW2) 138 % namespace eval sim {set str "A string"}
A string
(NEW2) 139 % info vars sim::* # no variable?
(NEW2) 140 % namespace eval sim {set ptr "A string"}
A string
(NEW2) 141 % info vars sim::*
::sim::ptr # but this one exists
On 2023-06-11, Helmut Giese wrote:
sorry if this is something obvious,
If my guess is correct, it kinda is, and I've stumbled on
exact same problem before.
but I don't know if I can trust my eyes. I have been sweating
several hours over the "problem" to get a command executed inside
a namespace - only to find out, that the cause seems to be
something totally different.
(NEW2) 137 % namespace eval sim {}
(NEW2) 138 % namespace eval sim {set str "A string"}
A string
(NEW2) 139 % info vars sim::* # no variable?
Do you perchance have a global str variable here? If so,
even though wrapped in namespace eval, 'str' above refers
to that /global/ variable.
(NEW2) 140 % namespace eval sim {set ptr "A string"}
A string
(NEW2) 141 % info vars sim::*
::sim::ptr # but this one exists
Conversely, as there're no ptr variable in the upper scope
(presumably), set ptr here creates a namespace variable.
The solution is to always use 'variable' within namespace eval
before referencing a namespace variable. Consider, e. g.:
set foo 0 ; namespace eval foo { variable bar ; set foo 1 ; set bar 2 ; } >puts [ info var foo::* ]
## results in:
::foo::bar
Hello out there,
sorry if this is something obvious, but I don't know if I can trust my
eyes. I have been sweating several hours over the "problem" to get a
command executed inside a namespace - only to find out, that the cause
seems to be something totally different.
Consider this screen dump (from within TKCon):
(NEW2) 136 % catch {namespace delete sim}
0
(NEW2) 137 % namespace eval sim {}
(NEW2) 138 % namespace eval sim {set str "A string"}
A string
(NEW2) 139 % info vars sim::* # no variable?
(NEW2) 140 % namespace eval sim {set ptr "A string"}
A string
(NEW2) 141 % info vars sim::*
::sim::ptr # but this one exists
(NEW2) 142 % namespace eval sim {set str2 "A string"}
A string
(NEW2) 143 % info vars sim::*
::sim::ptr ::sim::str2 # as does this one?
At this point I figured that maybe just the firstly created was
ignored and tried again
(NEW2) 144 % namespace eval sim {set str [dict create i 0 j 0]}
i 0 j 0
(NEW2) 145 % info vars sim::*
::sim::ptr ::sim::str2 # no, 'str' just isn't created
(NEW2) 146 % namespace eval sim {set d [dict create i 0 j 0]}
i 0 j 0 # same result as in '144'
(NEW2) 147 % info vars sim::*
::sim::d ::sim::ptr ::sim::str2 # but this variable exists
(NEW2) 148 % set sim::d
i 0 j 0 # and has the expected value
(NEW2) 149 %
Probably I have a blind spot. I would welcome some feedback.
Thank you
Helmut
Oh, this on Windows 10 64 under Tcl8.6.10
On 6/11/2023 10:06 AM, Helmut Giese wrote:
Hello out there,
sorry if this is something obvious, but I don't know if I can trust my
eyes. I have been sweating several hours over the "problem" to get a
command executed inside a namespace - only to find out, that the cause
seems to be something totally different.
Consider this screen dump (from within TKCon):
(NEW2) 136 % catch {namespace delete sim}
0
(NEW2) 137 % namespace eval sim {}
(NEW2) 138 % namespace eval sim {set str "A string"}
A string
(NEW2) 139 % info vars sim::* # no variable?
(NEW2) 140 % namespace eval sim {set ptr "A string"}
A string
(NEW2) 141 % info vars sim::*
::sim::ptr # but this one exists
(NEW2) 142 % namespace eval sim {set str2 "A string"}
A string
(NEW2) 143 % info vars sim::*
::sim::ptr ::sim::str2 # as does this one?
At this point I figured that maybe just the firstly created was
ignored and tried again
(NEW2) 144 % namespace eval sim {set str [dict create i 0 j 0]}
i 0 j 0
(NEW2) 145 % info vars sim::*
::sim::ptr ::sim::str2 # no, 'str' just isn't created
(NEW2) 146 % namespace eval sim {set d [dict create i 0 j 0]}
i 0 j 0 # same result as in '144'
(NEW2) 147 % info vars sim::*
::sim::d ::sim::ptr ::sim::str2 # but this variable exists
(NEW2) 148 % set sim::d
i 0 j 0 # and has the expected value
(NEW2) 149 %
Probably I have a blind spot. I would welcome some feedback.
Thank you
Helmut
Oh, this on Windows 10 64 under Tcl8.6.10
on windows 10, tcl 8.6.9, from console
()1 % catch {namespace delete sim}
1
()2 % namespace eval sim {}
()3 % namespace eval sim {set str "A string"}
A string
()4 % info vars sim::*
::sim::str
()5 % namespace eval sim {set ptr "A string"}
A string
()6 % info vars sim::*
::sim::str ::sim::ptr
()7 % namespace eval sim {set str2 "A string"}
A string
()8 % info vars sim::*
::sim::str ::sim::ptr ::sim::str2
()9 % namespace eval sim {set str [dict create i 0 j 0]}
i 0 j 0
()10 % info vars sim::*
::sim::str ::sim::ptr ::sim::str2
()11 % namespace eval sim {set d [dict create i 0 j 0]}
i 0 j 0
()12 % info vars sim::*
::sim::d ::sim::str ::sim::ptr ::sim::str2
()13 % set sim::d
i 0 j 0
()14 % info pa
8.6.9
I suggest you try with windows console, and try from a newly run program, >could be something funny happened in the previous 135 commands, or maybe >something in tkcon.
Here's the text as I pasted it in, i had to edit out one of your comments
catch {namespace delete sim}
namespace eval sim {}
namespace eval sim {set str "A string"}
info vars sim::*
namespace eval sim {set ptr "A string"}
info vars sim::*
namespace eval sim {set str2 "A string"}
info vars sim::*
namespace eval sim {set str [dict create i 0 j 0]}
info vars sim::*
namespace eval sim {set d [dict create i 0 j 0]}
info vars sim::*
set sim::d
> (NEW2) 139 % info vars sim::* # no variable?On 2023-06-11, Helmut Giese wrote:
Do you perchance have a global str variable here? If so,
even though wrapped in namespace eval, 'str' above refers
to that /global/ variable.
Hello out there,
sorry if this is something obvious, but I don't know if I can trust my
eyes. I have been sweating several hours over the "problem" to get a
command executed inside a namespace - only to find out, that the cause
seems to be something totally different.
Consider this screen dump (from within TKCon):
(NEW2) 136 % catch {namespace delete sim}
0
(NEW2) 137 % namespace eval sim {}
(NEW2) 138 % namespace eval sim {set str "A string"}
A string
(NEW2) 139 % info vars sim::* # no variable?
(NEW2) 140 % namespace eval sim {set ptr "A string"}
A string
(NEW2) 141 % info vars sim::*
::sim::ptr # but this one exists
(NEW2) 142 % namespace eval sim {set str2 "A string"}
A string
(NEW2) 143 % info vars sim::*
::sim::ptr ::sim::str2 # as does this one?
At this point I figured that maybe just the firstly created was
ignored and tried again
(NEW2) 144 % namespace eval sim {set str [dict create i 0 j 0]}
i 0 j 0
(NEW2) 145 % info vars sim::*
::sim::ptr ::sim::str2 # no, 'str' just isn't created
(NEW2) 146 % namespace eval sim {set d [dict create i 0 j 0]}
i 0 j 0 # same result as in '144'
(NEW2) 147 % info vars sim::*
::sim::d ::sim::ptr ::sim::str2 # but this variable exists
(NEW2) 148 % set sim::d
i 0 j 0 # and has the expected value
(NEW2) 149 %
Probably I have a blind spot. I would welcome some feedback.
Thank you
Helmut
Oh, this on Windows 10 64 under Tcl8.6.10
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 67:20:00 |
Calls: | 6,915 |
Files: | 12,379 |
Messages: | 5,431,811 |