Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Itcl: inconsistency of $this variable

Closed / Fixeda1d08eb5353fd6fc39ed8b480f011af7bd875666Created 10 years agoUpdated 10 years ago
Descriptionhtml2017-05-05 15:51:20

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:

    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` 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`.

Here is the output of the script above for two versions of 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


Also, see Donal Fellows' comment here - https://stackoverflow.com/questions/43768288/itcl-inconsistency-of-this-variable#43778283

dgpplain2017-05-05 18:51:31
Thank you for the report.

Until I find time to look at it, could you
try the combination of Itcl 3.4.3 with Tcl 8.6.6
which might help us isolate the Itcl changes
from the Tcl changes.
dgpplain2017-05-05 19:39:10
The following output suggests to me this is
an Itcl issue rather than a Tcl issue:

Tcl  version : 8.6.7
Itcl version : 3.4

==== worker_process_direct   ====
Direct query: this = ::worker

==== worker_process_indirect ====
Indirect query: this = ::worker

==== worker_process_both     ====
Direct query: this = ::worker
Indirect query: this = ::worker
dgpplain2017-05-05 20:24:43
There doesn't seem to be a need for Main
to inherit Base .  I remove that and nothing
changes.
dkfhtml2017-05-06 16:50:19

If I convert the code to basic TclOO as below, it appears to work correctly. I'm guessing therefore that the bug is in a resolver that itcl is installing. Variable resolvers are astonishingly difficult to write correctly.

puts "Tcl  version : $tcl_patchLevel"

oo::class create Base {				# <<< CHANGE <<<
    constructor {} {				# <<< CHANGE <<<
	variable this [self];			# <<< CHANGE <<<
    };						# <<< CHANGE <<<
    variable m_main;				# <<< CHANGE <<<
    method base_process {script} {
	uplevel [list $m_main main_process $script]
    }
    method set_main {main} {
	set m_main $main
    }
}

oo::class create Main {				# <<< CHANGE <<<
    superclass Base;				# <<< CHANGE <<<
    variable this;				# <<< CHANGE <<<
    method main_process {script} {
	uplevel $script
    }
}

oo::class create Worker {			# <<< CHANGE <<<
    superclass Base;				# <<< CHANGE <<<
    variable indirect this;			# <<< CHANGE <<<
    method worker_process_direct {} {
	puts "Direct query: this = $this"
	set indirect {puts "Indirect query: this = $this"}
    }
    method worker_process_inderect {} {
	my base_process $indirect;		# <<< CHANGE <<<
    }
    method worker_process_both {} {
	puts "Direct query: this = $this"
	my base_process $indirect;		# <<< CHANGE <<<
    }
}

Main create main;				# <<< CHANGE <<<

Worker create worker;				# <<< CHANGE <<<
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
However, I've studied the Tcl code in more depth and I now conclude that my dark mutterings about other bugs were in fact unfounded. True, I really don't know why the bytecode compilations vary so much, but change to the kinds of bytecodes issued was correct and deliberate. (Indeed, I remember doing the switch to using loadStk from loadScalarStk; that particular one was because we had two instructions doing the exactly identical thing.)

My guess is that Itcl's resolver is using the wrong stack frame in the interpreter to drive its resolution rules. (There are two confusingly-similar fields, for reasons I don't quite remember. I've been caught out by this in the past though, so I get it right now.)

dgpplain2017-05-09 13:56:40
See new Itcl ticket: https://core.tcl.tk/itcl/tktview/c45384364c

The reduced demo there establishes...
   1) Inheritance is not needed to see trouble.
   2) Special handling of the 'this' variable plays a role.
dgpplain2017-05-16 14:42:38
Fixed for release Itcl 4.1.0
to be bundled with Tcl 8.6.7.
Log in (anonymous is fine) to add a comment.