View · Search · Index
No registered users in community xowiki
in last 10 minutes

[Xotcl] Problem with automatic variable unsetting upon return from an instproc?

From: Jim Russell <Jim.Russell_at_dynetics.com>
Date: Tue, 18 Nov 2003 15:39:58 -0600

I've compiled XOTcl 1.0.2 with Tcl 8.4.4 to create a xotclsh. When I
execute the script below, the traced variables are unset once too few
times. Also, the order of the unsetting seems odd. Am I just confused,
or I have discovered a problem?

Here's the sample output:

russell> xotclsh factorial.xotcl
rhs = 3
rhs = 2
rhs = 1
tracecb "rhs" "" "unset" "2"
tracecb "rhs" "" "unset" "3"
tracecb "f" "" "unset" "::factorial-1"
tracecb "f" "" "unset" "::factorial-0"
3! = 6


>>>> File factorial.xotcl <<<<

proc tracecb { name1 name2 op } {
     upvar $name1 var
     puts "tracecb \"$name1\" \"$name2\" \"$op\" \"$var\""
}

xotcl::Class factorial;
factorial proc new { args } {
     eval my [ my autoname factorial- ] $args
}

factorial instproc compute { rhs } {
     puts "rhs = $rhs"
     if { $rhs > 1 } {
         set f [ factorial new ]
         ::trace add variable f [list unset] tracecb
         set lhs [ $f compute [ expr $rhs - 1 ] ]
     } else {
         set lhs 1
     }
     set product [ expr $lhs * $rhs ]
     ::trace add variable rhs [ list unset ] tracecb
     return $product
}

proc main { value } {
     set f [ factorial new ]
     puts "${value}! = [ $f compute $value ]"
}

main [expr [ llength $argv ] ? [ lindex $argv 0 ] + 0 : 3 ]