This seems very dangerous to me - perhaps you should consider not using this.[This is used frequently - someone want to explain why and when it is needed?]Isn't this just the Itcl counterpart to namespace code ?GWM The namespace code page says that it is a constructor for namespace inscope; inscope says "This command is not expected to be used directly by programmers". Thus I would expect that itcl::code should also not be used by (end user) programmers, although the construct might be used in supplied Tcl packages. The purpose of declaring a variable 'private' is to assure the programmer that there can be no unexpected modifications of the variable other than through the class interface methods.
- DKF: That would probably be a mistake. The purpose of itcl::code is to create a token that can be used from outside the object to call back into a method inside the object. There are a number of places you need this sort of thing (after callbacks, trace callbacks, widget callbacks, …). Note that these are places where there is something that is formally outside the object but which is used as part of the implementation of the object, and they are essential to the practical implementation of any object in Tcl (given our current scoping rules).
package require Itcl itcl::class hellobtn { public variable name "No-one" constructor {{frame ""}} { pack [ button $frame.hbtn -text "Hello World" -command "$this greet $frame.hbtn" ] } method greet {btn} { $btn config -background red -text $name } } foreach btn {"Don't" Press This Button} { pack [frame .f$btn] hellobtn h$btn .f$btn h$btn configure -name "$btn" }