When I run the script, I get:
::x y 'y0' ::x y 'y1' ::x init '' ::x y '' expected 'y1', but got: ''
Yes, you are right, I overload the setter/getter method and expected naively that it just prints the puts-statement and works otherwise unchanged. [Should have read the manual more properly, I guess :-)] I can achieve my desired result too, by doing something like:
X instproc y {args} { puts "[self] y '$args'" my instvar y if {[llength $args]} { set y [lindex $args 0] } next }
But I wanted to have an 'official' answer to this behaviour.
Regards, - Florian
-----Original Message----- From: Gustaf Neumann [mailto:neumann@wu-wien.ac.at] Sent: Thursday, December 08, 2005 12:43 PM To: Murr, Florian Cc: xotcl@alice.wu-wien.ac.at Subject: Re: [Xotcl] Is this a bug?
Hmm, are you sure? the script you have sent does not produce the message you are indicating....
~/scripts> set ::xotcl::version 1.3 ~/scripts> set ::xotcl::patchlevel .8 ~/scripts> Class X -parameter {
{y y0} }
::X ~/scripts> X instproc init {args} { puts "[self] init '$args'"; next } ~/scripts> X instproc y {args} {
set x [next] puts "[self] y '$args'" set x }
~/scripts> X create x -y y1 ::x y 'y0' ::x y 'y1' ::x init '' ::x ============================= however, most probably it is not doing what you might be expecting. your method y overwrites the setter method of parameter y, therefore in this example, not much happens in the next of method y.
maybe you had in mind to overload the setter/getter method, which you can do e.g. with a mixin (see below)
Class X -parameter { {y y0} } -instmixin [Class new -instproc y args { puts "[self] [self proc] $args" set x [next] puts "[self] [self proc] $args setter returns <$x>" return $x }]
X create x -y y1 puts x.y=[x set y]
Murr, Florian schrieb:
Yes, you are right, I overload the setter/getter method and expected naively that it just prints the puts-statement and works otherwise unchanged. [Should have read the manual more properly, I guess :-)]
for each parameter a method for getting and setting variables registered. This method is a "instparametercmd" implemented in C, which is very similar to the set method. This can be as well explained with the forwarder, which could be used to implement the *parametercmds
Class C C instforward x %self set %proc
now a method named x is defined, that calls "[self] set x" when issued. eg.
C c1 c1 x 123
here "c1 set x 123" is called, or in the following line "c1 set x" is issued.
puts [c1 x]
This behavior is more or less identical to
Class C C instparametercmd x
or to
Class C -parameter x
therefore, the instparamtercmd is strictly speaking not needed, but predates instforward and is slightly faster, since everything is in C. The parameters do more, since they can be used to provide as well default values which are used during initialization of objects.
anyhow, in all cases a method named "x" is defined. When you define an instproc named x
Class C C instproc x args {...}
you will overwrite the method "x", in a similar manner as you can overwrite any tcl command, e.g proc if args { ...}
Therefore, there is no chance to do a next in these cases, because the class has now the new method (instproc) instead of the c-level implementation we had earlier.
Furthermore "next" works only when methods are shadowed, when a same-named method is defined by a class/object earlier in the precedence order. This can be achieved e.g. via mixins or filters.
therefore, in your example
X instproc y {args} { puts "[self] y '$args'" my instvar y if {[llength $args]} { set y [lindex $args 0] } next }
the "next" is not doing anything useful, you should replace the last line of your instproc by e.g. "return $y", such that .... [x1 y] ... works
hope this explains, how this works...
-gustaf neumann