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
> 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?
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
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