從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
類的實例,而不是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,我還沒有發現顯著的變化?
那麼,你使用了不同的主要版本的itcl ...我可以用Tcl 8.6.3和Itcl 4.0.2重現。 –
** tl; dr **:您看起來像是發現了一個itcl錯誤。幸運的你。 –