if 0 {Richard Suchenwirth 2005-03-16 - Here's a little experiment in OO using the Jim feature that procs and lambdas can have a set of static variables associated. This way, the only physical location of an object is its lambda - rename it away as destructor. No namespaces needed (or possible in Jim ;^)}
#-- "Constructor"
proc Account args { lambda {method args} {{balance 0}} {eval Account'$method $args} }
#-- Methods:
proc Account'deposit amount { upvar 1 balance balance set balance [+ $balance $amount] } proc Account'see {} { upvar 1 balance balance set balance } proc Account'withdraw amount { upvar 1 balance balance if {($balance-$amount)<0} {error "can only withdraw $balance"} set balance [- $balance $amount] }
if 0 {Now testing:
set a [Account] puts a:$a $a deposit 100 puts "deposit 100 -> [$a see]" $a withdraw 50 puts [$a see] catch {$a withdraw 1000} res puts $res
shows on stdout
a:<reference.<functio>.00000000000000000000> deposit 100 -> 100 50 can only withdraw 50
SS Very nice! It seems to me a very natural hack to try to implement objects via closures. Also your way to do the dispatch is impressive ;)
Arts and crafts of Tcl-Tk programming Category Object Orientation Category Jim }
}