Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding the namespace from an imported command

3 views
Skip to first unread message

Brian Theado

unread,
Aug 2, 2003, 8:54:12 PM8/2/03
to
Is there a way to find the namespace which imported a command? For
instance in method1 below, I want to be able to find out if it was
called via the class1 namespace or the instance1 namespace. My goal
is to be able to modify variables in the instance1 namespace (and
whatever other namespace happens to import method1) from within
method1.

namespace eval class1 {
proc method1 {} {
puts "called function = [info level 0]"
puts "namespace origin = [namespace origin [info level 0]]"
puts "namespace current = [namespace current]"
}
namespace export method1
}
namespace eval instance1 {
namespace import ::class1::method1
}
namespace eval instance1 method1

output:
called function = method1
namespace origin = ::class1::method1
namespace current = ::class1

The only relevant namespace sub-commands I've found (orgin and
current) return the namespace where the procedure is defined.

Brian Theado

John Seal

unread,
Aug 3, 2003, 4:09:29 PM8/3/03
to
In article <43c754b5.03080...@posting.google.com>,
bth...@rrohio.com (Brian Theado) wrote:

> My goal
> is to be able to modify variables in the instance1 namespace (and
> whatever other namespace happens to import method1) from within
> method1.

Isn't that OO heresy? You should be able to change only those things
which the class deigns to give you methods to change. Or are you
writing some sort of "omniscient developer" introspection tool?

Mark G. Saye

unread,
Aug 3, 2003, 4:33:20 PM8/3/03
to Brian Theado

You can use a combination of uplevel and 'namespace which'. Hopefully
the modified script below shows you what I mean :

namespace eval class1 {
proc method1 {args} {
set proc [lindex [info level 0] 0]
puts "proc = $proc"
puts "args = [lrange [info level 0] 1 end]"
puts "namespace origin = [namespace origin $proc]"


puts "namespace current = [namespace current]"
}
namespace export method1

namespace export method2
}

proc class1::method2 {args} {
set proc [lindex [info level 0] 0]
puts "proc = $proc"
puts "which = [uplevel [list namespace which -command $proc]]"
}

namespace eval instance1 {
namespace import ::class1::method*
}

namespace eval instance1 method1 abc def
namespace eval instance1 method2 abc def

which gives the output:

proc = method1
args = abc def


namespace origin = ::class1::method1
namespace current = ::class1

proc = method2
which = ::instance1::method2

Note: it is better to define procs outside of the 'namespace eval'
construct (as I have done with method2), so they are byte-compiled.

--
Mark G. Saye
markgsaye @ yahoo.com

Brian Theado

unread,
Aug 3, 2003, 10:54:39 PM8/3/03
to
John Seal <john...@indy.net> wrote in message news:<johnseal-2CF328...@news06.east.earthlink.net>...

My higher (than above) goal is to add class functionality to "Thingy:
a one-liner OO system" (http://wiki.tcl.tk/1225) in a way that ends up
with xotcl-like (http://xotcl.org) syntax. So the implementation
details may be OO heresy, but the end result won't be any more
heretical than xotcl is :).

When a method is called for an object, the object should have some way
of accessing its own state. The methods for a class will be defined
in a class's namespace and then imported into each object's instance.
Even though the proc has been imported into another namespace, it
still executes in the context of the original namespace. So I was
looking for a way to get at the "object's" namespace.

It seems that Mark Saye has the solution in another post (thanks,
Mark!):


> use a combination of uplevel and namespace which

Brian Theado

0 new messages