• 'namespace eval' seems to not like some names ???

    From Helmut Giese@21:1/5 to All on Sun Jun 11 19:06:13 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ivan Shmakov@21:1/5 to All on Sun Jun 11 17:53:48 2023
    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

    --
    FSF associate member #7257 http://am-1.org/~ivan/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Sun Jun 11 22:26:42 2023
    Hello Ivan,
    thank you so much, you are totally right: I had a variable 'str' - as
    I practically always have.
    Once the mystery solved it looks so obvious - but without your help I
    would have looked for ages to solve it.
    Thanks again
    Helmut

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Helmut Giese on Sun Jun 11 13:22:33 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Sun Jun 11 22:34:23 2023
    Hello et99,
    thanks for your effort, without Ivan's answer I would have tried your
    way and it would have shed some light on this problem - but Ivan's
    idea was spot on: a 'variable' statement for every newly created
    variable - and the problem was gone.
    Thanks again
    Helmut

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to Ivan Shmakov on Sun Jun 11 22:26:56 2023
    On 11/06/2023 19:53, Ivan Shmakov wrote:
    On 2023-06-11, Helmut Giese wrote:
    > (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.

    To be fixed in Tcl 9.0: https://core.tcl-lang.org/tips/doc/trunk/tip/278.md


    Schelte.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerald Lester@21:1/5 to Helmut Giese on Mon Jun 12 09:28:10 2023
    On 6/11/23 12:06, 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


    Because the variable str existed in the global namespace before line 138!!

    Either unset it or use the variable command instead of the set command
    to set the value.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)