Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: e30484578a59df442b12a9e5c6699b9ba9f1bacc
Ticket: a1d08eb5353fd6fc39ed8b480f011af7bd875666
Itcl: inconsistency of $this variable
User & Date: dkf on 2017-05-06 16:50:19
Changes

  1. icomment:
    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 <i>astonishingly</i> difficult to write correctly.
    <pre>
    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
    </pre>
    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 <code>loadStk</code> from <code>loadScalarStk</code>; that particular one was because we had two instructions doing the exactly identical thing.)
    <p>
    My <b><i>guess</i></b> 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.)
    
  2. login: "dkf"
  3. mimetype: "text/html"
  4. username: "dkf"