if 0 { experiment to make tcl understand 'a = c' mathematical commands. Starts with a standard command 'realnumber' and its Cmd, cget and configure scripts then a test script exercises it to demonstrate its 'use'. } proc realnumber {w args} { ;# a variable global $w.props ;# an array of options specific to the dimValue 'class' # define option list and each default value array set $w.props {-value 0 } set textArgs {} ;# list of arguments not specific to the class foreach {opt val} $args { if {[array names $w.props $opt]!=""} {set options($opt) $val } else { lappend textArgs $opt $val } } eval interp create $w ;# create the "procedure" w interp hide {} $w # Install the alias: # realnumberCmd are sub-commands for realnumber class interp alias {} $w {} realnumberCmd $w foreach opt [array names options] { $w configure $opt $options($opt) } return $w ;# the original object } proc realnumberCmd {self cmd args} { switch -- $cmd { configure {eval realnumberConfigure $self $cmd $args} cget {eval realnumberCget $self $args} "=" { $self configure -value $args } "?" { return [$self cget -value] } } } proc realnumberConfigure {self cmd args} { # 3 cases: # # $args is empty -> return all options with their values # $args is one element -> return current values # $args is 2+ elements -> configure the options #puts "Config comd $self $cmd $args [llength $args]" global $self.props switch [llength $args] { 0 { ;# return all options set result [array names $self.props] return $result } 1 { ;# return argument values lappend opts [$self cget $args] return $opts } default { ;# >1 arg - an option and its value foreach {option value} $args { ;# go through each option: if {[array names $self.props $option]=="-value"} { set $self.props($option) [expr $value] } elseif {[array names $self.props $option]!=""} { # access global array element for each added option. set $self.props($option) $value } else { ;# try the default $option, $value for $self $self configure $option $value } } return {} } } } proc realnumberCget {self args} { ;# cget defaults done by the interp cget command upvar #0 $self.props props ;# get local address for global array if {[array names props $args ]!=""} { return $props($args) } return 1 ;# [uplevel 1 [list interp invokehidden {} $self cget $args]] } proc test {} { realnumber biff set i 1 while {$i<12} { set j 1 while {$j<12} { biff = $i*$j puts "$i*$j is [biff ?]" ;# or use [biff cget -value] incr j } incr i } puts "factorials up to 14 NB greater than 16 this algorithm gets into trouble with integer overruns" set j 1 biff = 1 while {$j<14} { biff = [biff ?]*$j puts "$j! is [biff ?]" ;# or use [biff cget -value] incr j } } console show; update idletasks test