No registered users in community xowiki
in last 10 minutes
in last 10 minutes
Re: [Xotcl] xotcl namespace auto-import
From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Tue, 25 May 2004 10:43:07 +0200
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.
> 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}.
As a sidenote: not every xotcl obj creates a namespace, since
tcl namspaces are quite large in memory. Namespaces are
only created when an object has child objects or procs. You
can measure the difference on your system (the following works
for linux, ps might be different on other OSes)
==============================================
::xotcl::Class Mem -instproc init {} {
set r [exec ps -xv | fgrep [pid]]
::xotcl::my set m [lindex $r 6]
}
set n 1000
Mem m1
set ts [time {for {set i 0} {$i<$n} {incr i} {
::xotcl::Object create a$i}}]
Mem m2
set mem1 [expr {([m2 set m]-[m1 set m])*1024/$n}]
set ts [time {for {set i 0} {$i<$n} {incr i} {
::xotcl::Object create b$i -requireNamespace}}]
Mem m3
set mem2 [expr {([m3 set m]-[m2 set m])*1024/$n}]
puts "consumption/obj without ns: $mem1, with ns: $mem2"
==============================================
> 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. 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
here are such code snipplets:
==================================
namespace eval myns {
namespace import ::xotcl::*
Class MyClass
MyClass instproc test {} {
puts stderr "in [xotcl::self] current namespace [namespace current]"
puts "unprefixed: [self] [self class]"
}
MyClass o
}
puts =========
namespace eval myns { o test }
puts =========
namespace eval xotcl {myns::o test}
puts =========
==================================
if you have a component with a slim interface, you can
simply do a namespace eval once on the top calling level, this
does not cost much performance.
... 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.
-gustaf
Date: Tue, 25 May 2004 10:43:07 +0200
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.
> 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}.
As a sidenote: not every xotcl obj creates a namespace, since
tcl namspaces are quite large in memory. Namespaces are
only created when an object has child objects or procs. You
can measure the difference on your system (the following works
for linux, ps might be different on other OSes)
==============================================
::xotcl::Class Mem -instproc init {} {
set r [exec ps -xv | fgrep [pid]]
::xotcl::my set m [lindex $r 6]
}
set n 1000
Mem m1
set ts [time {for {set i 0} {$i<$n} {incr i} {
::xotcl::Object create a$i}}]
Mem m2
set mem1 [expr {([m2 set m]-[m1 set m])*1024/$n}]
set ts [time {for {set i 0} {$i<$n} {incr i} {
::xotcl::Object create b$i -requireNamespace}}]
Mem m3
set mem2 [expr {([m3 set m]-[m2 set m])*1024/$n}]
puts "consumption/obj without ns: $mem1, with ns: $mem2"
==============================================
> 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. 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
here are such code snipplets:
==================================
namespace eval myns {
namespace import ::xotcl::*
Class MyClass
MyClass instproc test {} {
puts stderr "in [xotcl::self] current namespace [namespace current]"
puts "unprefixed: [self] [self class]"
}
MyClass o
}
puts =========
namespace eval myns { o test }
puts =========
namespace eval xotcl {myns::o test}
puts =========
==================================
if you have a component with a slim interface, you can
simply do a namespace eval once on the top calling level, this
does not cost much performance.
... 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.
-gustaf
-- Univ.Prof. Dr.Gustaf Neumann Abteilung für Wirtschaftsinformatik WU-Wien, Augasse 2-6, 1090 Wien