[Xotcl] poll for opinions
Gustaf Neumann
neumann@wu-wien.ac.at
Fri, 22 Feb 2002 21:37:22 +0100
> Hm, what about this little baby:
>
> proc ::xotcl::gc {name1 name2 op} {
>
> ::uplevel $name1 destroy
>
> }
>
> Class instproc private {varname args} {
>
> ::upvar $varname v
> ::set v $varname
> ::uplevel trace variable $varname u ::xotcl::gc
> ::eval [self] create $varname $args
>
> }
>
Hi Zoran,
very nice!
...below is a still simpler version...
with the same
idea, we we can make a bind command, where we
can bind an object to a variable in the sense
that
a) the object is deleted, when the variable is
unset
b) the object can provide a print-value, when
someone read the value of the variable
this might be useful for e.g. associations as well,
where you have an instance variable refering to
an object, and once you delete the object (with its
variables) you want to destroy the referred object
(or decrement a reference counter)
-gustaf
PS: i have a little bad feeling about filters/mixins
and uplevel/upvar and friends.
#### private
proc ::xotcl::gc {name1 name2 op} {
puts "destroying $name1"
$name1 destroy
}
Class instproc private {varname args} {
::uplevel trace variable $varname u ::xotcl::gc
::uplevel set $varname [::eval [self] create $varname $args]
}
##### bind
proc ::xotcl::value {name1 name2 op} {
uplevel set $name1 [list [$name1 value]]
}
Object instproc bind {object varname} {
::uplevel trace variable $varname u ::xotcl::gc
::uplevel trace variable $varname r ::xotcl::value
::uplevel set $varname $object
}
#### usage
Class foo
foo private bar
Class C -parameter i
C instproc value {} {
return "[self]'s value is [[self] incr i]"
}
Object o
o proc test1 {} {
Object private o1
puts ...
o1 set x 1
puts ...
}
o proc test2 {} {
C o2 -i 11
puts ...
[self] bind o2 o2
puts ...$o2...
puts ...$o2...
}
puts =====
o test1
puts =====
o test2
puts =====