Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 27564a0c901e36cf0d9d0d17256c36f50271675f
Ticket: a1d08eb5353fd6fc39ed8b480f011af7bd875666
Itcl: inconsistency of $this variable
User & Date: GrAnd on 2017-05-05 15:51:20
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/html"
  4. comment changed to:
    <p>
    While doing my project migration from Tcl 8.5.9/Itcl 3.4 to Tcl 8.6.6/Itcl 4.0.5 I've faced with inconsistency of `$this` variable depending on how it's accessed. Here is the minimized testcase:
    </p>
    <pre>
        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
    </pre>
    
    <p>
    `worker_process_direct` and `worker_process_both` functions always provide correct results. But `worker_process_inderect` works correctly only with old version of Tcl/Itcl. For Tcl 8.6.6/Itcl 4.0.5 `$this` variable is strangely changed to the instance of `Main` class instead of `Worker`.
    </p>
    <p>
    Here is the output of the script above for two versions of Tcl/Itcl.
    </p>
    <hr width=150 align=left>
    <pre>
        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
    </pre>
    <hr width=150 align=left>
    <pre>
        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
    </code>
    <hr width=150 align=left>
    <p>
    Also, see Donal Fellows' comment here - <a href="https://stackoverflow.com/questions/43768288/itcl-inconsistency-of-this-variable#43778283">https://stackoverflow.com/questions/43768288/itcl-inconsistency-of-this-variable#43778283</a>
    </p>
    
  5. foundin changed to: "8.6.6"
  6. is_private changed to: "0"
  7. login: "GrAnd"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "d2848f54ebf917fc6fb2a00b4ba4b2271563347e"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "GrAnd"
  14. subsystem changed to: "07. Variables"
  15. title changed to: "Itcl: inconsistency of $this variable"
  16. type changed to: "Bug"