[Xotcl] xotcl namespace auto-import
Neil Madden
nem at cs.nott.ac.uk
Tue May 25 15:13:05 CEST 2004
Thanks for the reply. Comments below.
On 25 May 2004, at 09:43, Gustaf Neumann wrote:
> On Monday 24 May 2004 22:57, Neil Madden wrote:
>> Hello all,
>>
>> Small feature request: could XOTcl be made to automatically [namespace
>> import] the ::xotcl::* commands into every object it creates?
>> ...
>> 1) Fully qualify everything - lots of xotcl::my, xotcl::self etc
>> everywhere
>> 2) Import xotcl namespace manually into each object namespace.
>
> or 3) do a gobal namespace import.
That's generally not considered good style when writing a package - the
global namespace is "owned" by the application author, and so packages
shouldn't put stuff there unless explicitly asked.
>
>> 2 is better than 1, but is still a bit of a pain to remember to do,
>> and
>> I seem to remember trying it and it not always working (I could be
>> wrong about that).
>
> (2) does not help too much, since xotcl is "namespace transparent",
> which means that it simply keeps the current namespace.
> This means that if you do a "namespace eval xxx {o m}"
> the method is evaled in the namespace xxx (and not o}.
OK. That's good to know. But this namespace transparency does not
extend all the way. For instance:
package require XOTcl
namespace eval foo {
proc bar {} { puts "ns = [namespace current]" }
xotcl::Class Bar
Bar instproc foo {} { puts "ns = [namespace current]" }
Bar a
}
foo::bar ;# prints "ns = ::foo"
foo::a foo ;# prints "ns = ::"
I would expect that any object I create in a namespace will always be
evaluated in that namespace, as is the case for procs. Given the
namespace transparency you describe above, this would solve all my
problems.
<snip memory usage discussion>
>> So, my feature request would be that all Objects [namespace import]
>> the
>> xotcl namespace automatically.[1]
>
> we had something like this in earlier versions of xotcl, but it was
> quite
> a pain, where it was quite easy to run into inifine loops, since
> procs of objects were resolved without the object name. To be on
> the safe side, every reference to a gobal tcl command had to be
> prefixed by a :: (eg: ::set x 1), otherwise an "obj proc set .."
> was overruling the tcl command.
I'm not sure what you mean here. I wasn't suggesting that methods
should be able to be called without passing a message to self, just
that "my", "self" etc should be imported into each object.
> Note that we have multiple
> command sets in xotcl, not only in the procs, but in the instprocs
> of classes (superclasses, mixins) as well.
>
> you can do the following as other alternatives:
> a) you can import the xotcl commands into your
> components workspace, and do a namespace eval
> there
> b) you can eval the object in the ::xotcl namespace
Neither of these alternatives are particularly appealing, as they
require the consumer of my package to know to do this, and it would
clutter up their code quite a bit. I suppose I could write a wrapper
proc for each object:
xotcl::Class MyClass -superclass xotcl::Class
MyClass instproc create {name args} {
eval [linsert $args 0 next ${name}_]
uplevel 1 [list proc $name {args} "uplevel 1 \[linsert \$args 0
${name}_\]"]
}
But this still feels like something XOTcl should be doing for me.
> ... and, you can write a global unknown handler, but that is not
> better than importing xotcl (or at least self+my) globally, what
> still looks like the best option to me.
In 8.5 (hopefully) my [namespace unknown] TIP (181) will get in, so you
could do this per-namespace. But it is still a hack. For now, I will
import into my package namespace, and use wrapper procs.
Neil.
More information about the Xotcl
mailing list