[Xotcl] Re: [Xotcl] Classes and namespace

Uwe Zdun uwe.zdun at uni-essen.de
Tue Jan 30 14:06:56 CET 2001


On Monday 29 January 2001 23:54, Kristoffer Lawson wrote:
> [~] package require XOTcl
> 0.83
> [~] namespace eval Foo {
>
> >Class MyClass
> >MyClass instproc init {} {
> >AnotherClass newChild
> >}
> >Class AnotherClass
> >}
>
> AnotherClass
> [~] Foo::MyClass new
> invalid command name "AnotherClass"
> while evaluating {Foo::MyClass new}
>
> Ie. it appears as if methods aren't run in the same namespace as
> where classes exist (or a child namespace of this). I know this isn't
> exactly a bug (as I don't think it's specified anywhere?), but I believe
> the natural behaviour would be for AnotherClass to be found in the example
> above, as it's specified within the same namespace as Foo.
>
> Is the any chance the current behaviour could be altered to one that I
> think is more intuitive? Or is there a good reason why not?
>

XOTcl classes actually consist of two namespaces, one for the class object 
(where procs and class variable are defined) and one for the class features 
(where the instprocs are defined). The later one is "hidden" in the namespace
"XOTclClasses". So during execution of the "init" method the current namespace
is:

::XOTclClasses::Foo::MyClass       

Usually -- as a user of XOTcl -- you don't have to be aware of that
namespace separation. We probably should document it more prominently
in the tutorial.

But in the given case its not hard to find out where the 
AnotherClass command resides, because its in the parent namespace
of "MyClass":

namespace eval Foo {
  Class MyClass
  MyClass instproc init {} {
    [[self] info parent]::AnotherClass newChild
  }
  Class AnotherClass
}
Foo::MyClass new  

BTW, here, the ::XOTclClasses namespace ain't the problem ... but the Tcl 
namespace resolution. E.g., we expect:


  Object p
  p proc putHello {} {puts Hello}
  Object o
  o proc putHello {} {
    p putHello
  }
  o putHello

to work because Tcl first searches the current Namespace and then the global
namespace. This wouldn't functions here either if we surround it with a 
namespace definition:

namespace eval Foo {
  Object p
  p proc putHello {} {puts Hello}
  Object o
  o proc putHello {} {
    p putHello
  }
}
Foo::o putHello

But, again info parent works ;)

Regards,

Uwe

-- 
Uwe Zdun
Specification of Software Systems, University of Essen
Phone: +49 201 81 00 332, Fax: +49 201 81 00 398
zdun at xotcl.org, uwe.zdun at uni-essen.de




More information about the Xotcl mailing list