在将我的项目从 Tcl 8.5.9/Itcl 3.4 迁移到 Tcl 8.6.6/Itcl 4.0.5 时,我遇到了$this
变量不一致的问题,具体取决于它的访问方式。这是最小化的测试用例:
puts "Tcl version : $tcl_patchLevel"
puts "Itcl version : [package require Itcl]"
itcl::class Base {
public {
method base_process {script} {
uplevel $m_main main_process [list $script]
}
method set_main {main} {
set m_main $main
}
}
protected {
variable m_main
}
}
itcl::class Main {
inherit Base
public {
method main_process {script} {
uplevel $script
}
}
}
itcl::class Worker {
inherit Base
public {
method worker_process_direct {} {
puts "Direct query: this = $this"
}
method worker_process_inderect {} {
base_process {puts "Indirect query: this = $this"}
}
method worker_process_both {} {
puts "Direct query: this = $this"
base_process {puts "Indirect query: this = $this"}
}
}
}
Main main
Worker worker
worker set_main main
puts "\n==== worker_process_direct ===="
worker worker_process_direct
puts "\n==== worker_process_indirect ===="
worker worker_process_inderect
puts "\n==== worker_process_both ===="
worker worker_process_both
worker_process_direct
和worker_process_both
函数总是提供正确的结果。但worker_process_inderect
仅适用于旧版本的 Tcl/Itcl。对于 Tcl 8.6.6/Itcl 4.0.5 $this
,变量奇怪地更改为Main
class 的实例而不是Worker
.
这是上面两个版本的 Tcl/Itcl 脚本的输出。
Tcl version : 8.5.9
Itcl version : 3.4
==== worker_process_direct ====
Direct query: this = ::worker
==== worker_process_indirect ====
Indirect query: this = ::worker <<<<<<<<<<<< CORRECT
==== worker_process_both ====
Direct query: this = ::worker
Indirect query: this = ::worker
Tcl version : 8.6.6
Itcl version : 4.0.5
==== worker_process_direct ====
Direct query: this = ::worker
==== worker_process_indirect ====
Indirect query: this = ::main <<<<<<<<<< INCORRECT
==== worker_process_both ====
Direct query: this = ::worker
Indirect query: this = ::worker
我是否遗漏了一些东西,并且 Tcl/Itcl 发生了我没有注意到的重大变化?