View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] Error or normal behavior?

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Wed, 08 Dec 2010 08:31:52 +0100

On 08.12.10 07:35, Victor Mayevski wrote:
> Thank you Gustaf,
>
> I guess I just expected that things will be automatically tracked no
> matter in which namespace I invoked the nx::Class command.
maybe your assumption was that since you create a class ::C with
"nx::Class", then init of C will run in the "nx" namespace;
... but it is not, it is running in ::.

The reason, why using nx there is probably not a good
idea is a follows: Consider that one might want to create e.g.
other cmds (objects/procs/...) from a method without
explicit namespaces. If the current namespace would be
nx, these cmds would be created in the nx namespace,
probably not always desired.

XOTcl/nx tries to follow Tcl with the namespace rules.
If a proc foo is contained in a namespace X
(place of definition), then the current namespace in foo is X.
For objects/classes this is not so obvious, but similar.
If a class is created in a namespace X, its methods are
executed in that namespace.

============================================
    package req nx

    #
    # your application namespace
    #
    namespace eval ::app {
       proc foo {} {puts proc-current=[namespace current]}

       nx::Class create C
       C method init args {puts current=[namespace current]}
    }

    #
    # using the definitions
    #
    ::app::foo
    ::app::C create c1
============================================

The behavior is the same if ::app::foo or ::app::C are
created from some other namespace (eg the
toplevel namespace) with the explicit namespace
in the name. So, not the namespace of the invocation
is important, but the namespace of the definition (which
certainly might be the same).

Btw, one can use "namespace import ::nx" or
"namespace path" not only in the global namespace,
but in the namespace of definition.

============================================
     package req nx

    #
    # your application namespace
    #
    namespace eval ::app {
       namespace path ::nx

       nx::Class create C
       C method init args {puts self=[self]; next}
    }

    #
    # using the definitions
    #
    ::app::C create c1
============================================

Hope, this helps a little....

-gustaf neumann