On Friday 24 January 2003 04:32, Kristoffer Lawson wrote:
Been thinking if this would make sense:
while {$stuff} { tie myVar [MyClass new] ... }
And the instance created from MyClass would be automagically collected on each iteration. Ie. [tie] would tie an object to a variable: when the variable's value is changed, or when the variable is destroyed, the object is too -- or at least the refcount decreased. I often have Message objects which are used once (after some data is read from somewhere) and then destroyed. Quite often I forget the latter part ;-)
/ http://www.fishpool.com/~setok/
there are already many approaches for destroying object automatically. look at the following code (hope there are not to many typos)
Class C C instproc destroy {} {puts "[self] destroy"; next}
C c1 c1 proc t1 {} { for {set i 0} {$i < 10} {incr i} { O new -volatile } } c1 proc t2 {} { for {set i 0} {$i < 10} {incr i} { O new -childof [self] } } c1 proc t3 {} { for {set i 0} {$i < 10} {incr i} { O [self]::$i } } c1 proc t4 {} { for {set i 0} {$i < 10} {incr i} { O [self]::i } }
-t1: after the loop, all objects exist, the automatically destroyed, when t1 is left. -t2: all objects survive t2, they are deleted, when c1 is destroyed -t3: same as t2, but the programmer has more control over object names (see also t4) -t4: on each iteration an object is created and destroys a potentially same-named object. after t4, c1 contains one sub-object, which is destroyed, when c1 is destroyed (this is somewhat similar in the behavior to the code that uwe posted).
we could provide an option -bind <varname> to the new instproc to allow the user to specify a local or a per-object or global variable name, but this does not provide reference counting at all. i have started some time ago to work on reference counting, and xotcl has some good prerequirements for this: we have tcl_objs for xotcl objects, they maintain already a reference count. the obvious idea is to destroy the object, when the reference count reaches 0. however, practically this showed to be quite tricky in the current implementation since the refcount = 0 condition happens in some unvonveniant situations... i have not given up on this, but it needs a bigger chunk of time to devote to this...
best regards -gustaf
Xotcl mailing list - Xotcl@alice.wu-wien.ac.at http://alice.wu-wien.ac.at/mailman/listinfo/xotcl