Re: Incr Tcl: Something like a scoped object
- From: suchenwi <richard.suchenwirth-bauersachs@xxxxxxxxxxx>
- Date: Fri, 30 Nov 2007 00:33:46 -0800 (PST)
On 30 Nov., 08:46, antred <Nut...@xxxxxxx> wrote:
Hello,
Does Incr Tcl offer something like scoped variables in C++ or context
managers in Python? That is, an object that, as soon as it goes out of
scope, automatically has a certain finalization method invoked on it?
I'd find that immensely useful if it did exist.
An example:
::itcl::class SomeClass {
public destructor {} {
puts "Destructor called."
}
}
proc someProc {} {
set myScopedObject [ SomeClass \#auto ]
# Do tons of interesting stuff.
# ...
# ...
# myScopedObject goes out of scope ... destructor is invoked
automatically.
}
That's how I'd like it to be. Unfortunately the way it is I have to
call object delete $myScopedObject to get the destructor invoked.
Don't get me wrong, I don't want to override the standard behavior for
all class objects, but in some cases it would be really nice. Without
this ability, use of RAII pattern is pratically impossible in Tcl,
which I find a serious limitation.
You might try an unset trace:
proc myProc {} {
set myObj [SomeClass \#auto]
trace var myObj u "object delete $myObj ;#"
#... interesting stuff to follow
}
Note that the ;# at the end of the trace are necessary to clip off
trailing arguments - and $myObj must be substituted at trace
definition time, because the variable is gone then already.
.
- References:
- Incr Tcl: Something like a scoped object
- From: antred
- Incr Tcl: Something like a scoped object
- Prev by Date: Incr Tcl: Something like a scoped object
- Next by Date: Re: Incr Tcl: Something like a scoped object
- Previous by thread: Incr Tcl: Something like a scoped object
- Next by thread: Re: Incr Tcl: Something like a scoped object
- Index(es):
Relevant Pages
|