According to the ITcl man page a delegate method can be declared with delegate method methodName to componentName ?as targetName? (https://www.tcl.tk/man/tcl/ItclCmd/itcldelegate.html#M6).
In the below code, for the Dog class there are two variables tail and tail2 that get intialised to a Tail object. However only the wag2 method retains the correct mood.
I want to clarify whether the delegation declares component tail as a static variable ?
(FYI this is a followup question to a comment from How to use itcl::delegation in TCL?).
package require itcl
itcl::extendedclass Tail {
variable mood
constructor {_mood} {
set mood $_mood
}
method wag {} {
return "Wag, wag, wag $mood"
}
}
itcl::extendedclass Dog {
delegate method wag to tail
variable tail2
constructor {mood} {
set tail [namespace which [Tail #auto $mood]] ;#// https://stackoverflow.com/questions/68336192/how-to-use-itcldelegation-in-tcl
set tail2 [Tail #auto $mood]
}
method wag2 {} {
return [$tail2 wag]
}
}
puts [info patchlevel]
puts $itcl::patchLevel
Dog happydog :)
Dog saddog :(
puts [happydog wag]
puts [saddog wag]
puts [happydog wag2]
puts [saddog wag2]
Expected:
Wag, wag, wag :)
Wag, wag, wag :(
Wag, wag, wag :)
Wag, wag, wag :(
Actual:
Wag, wag, wag :( <==== note: happydog is not happy
Wag, wag, wag :(
Wag, wag, wag :)
Wag, wag, wag :(
Screenshot of output (that shows only the wag2 call retains correct mood):
