Version 0 of OO Method calls

Updated 2004-02-22 18:18:25

These are some implementations of the method invocation benchmark of Tcl OO Bench.


ITcl

 package require Itcl

 itcl::class Toggle {
   variable state
   constructor {start_state} {
     set state $start_state
   }
   method value {} {
     return $state
   }
   method activate {} {
     set state [expr {!$state}]
     return $this
   }
 }

 itcl::class NthToggle {
   inherit Toggle
   variable count_max
   variable counter
   constructor {start_state max_counter} {
     Toggle::constructor $start_state
   } {
     set count_max $max_counter
     set counter 0
   }
   method activate {} {
     if {[incr counter] >= $count_max} {
       Toggle::activate
       set counter 0
     }
     return $this
   }
 }

 proc main {} {
   set n [lindex $::argv 0]
   set val 1
   set toggle [Toggle #auto $val]
   for {set i 0} {$i < $n} {incr i} {
     set val [[$toggle activate] value]
   }
   if {$val} {puts "true"} else {puts "false"}

   set val 1
   set ntoggle [NthToggle #auto 1 3]
   for {set i 0} {$i < $n} {incr i} {
     set val [[$ntoggle activate] value]
   }
   if {$val}  {puts "true"} else {puts "false"}
 }

 main

---