Hello,
Why in the following code launched from tclsh I get the error: invalid command name "Attribute" during '::test1::Test slots'
###### package provide test1 0.1 package require XOTcl
namespace eval ::test1 { namespace import ::xotcl::* Class Test -slots { Attribute name } namespace export Test } namespace import ::test1::* ######
XOTcl 1.6.4, works ok from xotclsh and when 'namespace import ::xotcl::*' comes right after 'package require XOTcl' and not within test1 namespace.
Thanks.
Mykhaylo,
XOTcl 1.6.4, works ok from xotclsh and when 'namespace import ::xotcl::*' comes right after 'package require XOTcl' and not within test1 namespace.
This behaviour is due to an interaction between the implementation strategy for slots as implemented in XOTcl 1.5+ and the standard namespace/command resolution rules of Tcl.
(::xotcl::)Attribute is declared and resolved for the scope of a per-object namespace ::test1::Test::slot. This namespace is acquired upon calls to the slot method of ::xotcl::Class. You may find out by inspecting the return value of [namespace current] (see below). While this is merely an implementation detail and should not concern the XOTcl developer, it interferes with the way Tcl resolves relatively-qualified command (and variable) names. While slightly more complex at second sight, generally speaking, the command name (e.g., Attribute) is first searched for in the current ns (::test1::Test::slot) and then in the global one (::):
see Section "NAME RESOLUTION" at http://docs.activestate.com/activetcl/8.5/tcl/TclCmd/namespace.htm#M18
Intermittent ones are skipped (e.g., ::test1, ::test1::Test). Hence the behaviour you are observing. This also explains why a [namespace import ::xotcl::*] in the /global/ ns (e.g., following [package req XOTcl]) is a valid fix.
You may also consider:
1. fully-qualified command names: ::xotcl::Attribute 2. a preceding [namespace import ::xotcl::*] in the '-slots' block (see below) 3. use of [namespace path] starting with 8.5 (see below)
//stefan
###### package provide test1 0.1 package require XOTcl
# ::test1 namespace eval ::test1 { # # [namespace current] -> ::test1 # namespace import ::xotcl::* # alternatively: namespace path ::xotcl Class ::test1::Test -slots { # # [namespace current] -> ::test1::Test::slot # namespace import ::xotcl::* # alternatively: namespace path ::xotcl Attribute name } namespace export Test } namespace import ::test1::*
######
Dear Mykhaylo,
The "slots" method changes the namespace to make it convenient for the user to write "Attribute name" and not "Attribute test1::Test::slot::name".
The following snipped shows a simple solution, but this is not nice either. With Tcl 8.5, one could use instead of the "namespace import ..." "namespace path ::xotcl" before "Attribute...".
namespace eval ::test1 { namespace import ::xotcl::* Class Test -slots { namespace import ::xotcl::* Attribute name } namespace export Test }
This solution works, but is not nice either. If you include the "contains" method below in your script, your original example will work as expected. I am planning to use this definition in the 1.6.5 release.
best regards -gustaf neumann
============================== ::xotcl::Object instproc contains { {-withnew:boolean true} -object {-class ::xotcl::Object} cmds} { if {![info exists object]} {set object [::xotcl::self]} if {![::xotcl::my isobject $object]} { $class create $object $object requireNamespace namespace eval $object {namespace import ::xotcl::*} } if {$withnew} { set m [::xotcl::ScopedNew new \ -inobject $object -withclass $class -volatile] ::xotcl::Class instmixin add $m end namespace eval $object $cmds ::xotcl::Class instmixin delete $m } else { namespace eval $object $cmds } } ==============================
Mykhaylo Sorochan schrieb:
Hello,
Why in the following code launched from tclsh I get the error: invalid command name "Attribute" during '::test1::Test slots'
###### package provide test1 0.1 package require XOTcl
namespace eval ::test1 { namespace import ::xotcl::* Class Test -slots { Attribute name } namespace export Test } namespace import ::test1::* ######
XOTcl 1.6.4, works ok from xotclsh and when 'namespace import ::xotcl::*' comes right after 'package require XOTcl' and not within test1 namespace.
Thanks.