RosettaCodeData/Task/Add-a-variable-to-a-class-instance-at-runtime/Tcl/add-a-variable-to-a-class-instance-at-runtime-1.tcl
2013-04-10 14:58:50 -07:00

27 lines
486 B
Tcl

% package require TclOO
% oo::class create summation {
constructor {} {
variable v 0
}
method add x {
variable v
incr v $x
}
method value {{var v}} {
variable $var
return [set $var]
}
destructor {
variable v
puts "Ended with value $v"
}
}
::summation
% set s [summation new]
% # Do the monkey patch!
% set [info object namespace $s]::time now
now
% # Prove it's really part of the object...
% $s value time
now
%