[Xotcl] forwarding

Gustaf Neumann neumann at wu-wien.ac.at
Tue Jan 24 09:01:16 CET 2012


Dear Victor,

The best approach for forwarding to a child object is either 
an alias, or a direct invocation of a subobject through the 
method interface. Consider the following code, where we have 
an object "obj" and an embedded child object "obj::child".

    nx::Object create obj {

       nx::Object create [self]::child {
       :public method foo {} {return [self]}
     }

     :public alias   a [self]::child
     :public forward f [self]::child
    }


One could certainly use as well a forward, but this is 
necessary. Technically, objects are Tcl commands, and Tcl 
commands and procs reside in a namespace. Therefore, by 
creating a child object of obj, one has to create in its 
namespace a cmd "child", which is in turn very similar to a 
per-object method. Therefore, one cannot define a per-object 
method "foo" and a child object "foo" at the same time.

The idiom to use an object as a container for subobject and 
delegate to these is quite common in some systems (used 
intensively in xotclIDE). In our previous releases of nx, 
the semantics of calling of the subobject have changed 
relative to XOTcl 1.6 due to the requirements of ensemble 
objects. The recent changes reintroduced backwards 
compatibility in this respect as well, leading to a probably 
more expected behavior in the case above (concerning "self" 
in the methods of the subobject). As you can see below the 
invocation of the child is the fastest approach.

   ? {obj a foo} {::obj::child}
   ? {obj f foo} {::obj::child}
   ? {obj child foo} {::obj::child}

v/child-obj-delegation.001:      0.71    mms, obj a foo 
(overhead 0.62)
v/child-obj-delegation.002:      0.75    mms, obj f foo 
(overhead 0.65)
v/child-obj-delegation.003:      0.65    mms, obj child foo 
(overhead 0.68)

Note, that this requires the head version of nx (which works 
now as well with Tcl version from fossil head); we will tag 
a new beta release soon, documentation updates are pending.

In case, one does not want to allow calls of the subobject 
via the method interface (3rd case above), one can use 
access control ("private" or "protected"). Maybe we add for 
nx some more syntactic sugar by allowing something along the 
lines of

    nx::Object create obj {

     :public child nx::Object bar {
        :public method foo {} {return [self]}
     }

     :public alias   a [self]::child
     :public forward f [self]::child
    }


for creating a child object "obj::bar" with public access.

-gustaf neumann

On 11.01.12 04:06, Victor Mayevski wrote:
> Is it possible to do a forward in the following manner:
>
>       obj public forward link %self::child-obj
>
> The best I could do was:
>
>       obj public forward link :child-obj
>
> which I have to call in the following way:
>       [obj link] mymethod
>
> Also, I don't even know why it works that way, because I think it
> should work this way:
>       obj public forward link -objframe :child-obj
> but that does not work.
>
> Thanks
> _______________________________________________
> Xotcl mailing list
> Xotcl at alice.wu-wien.ac.at
> http://alice.wu-wien.ac.at/mailman/listinfo/xotcl



More information about the Xotcl mailing list