Version 0 of Tiny OO with Jim

Updated 2005-03-16 14:54:17 by suchenwi

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

Arts and crafts of Tcl-Tk programming }

}