Re: Incr Tcl: Something like a scoped object



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.
.



Relevant Pages

  • Re: Object destruction
    ... MyObject * myObj; ... myObject is destroyed when it goes out of scope in main. ... destructor (or, in a sense, it has a trivial destructor that does ...
    (microsoft.public.vc.language)
  • Re: Object destruction
    ... MyObject * myObj; ... myObject is destroyed when it goes out of scope in main. ... destructor (or, in a sense, it has a trivial destructor that does ...
    (microsoft.public.vc.language)
  • Re: Overcoming C# limitations
    ... You might as well have defined a CString and have a ... "hidden" destructor as you call it run when the function leaves. ... This would have been useful since I could use this macro in any scope ... knowing that the destructor of MyObj will be called at ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overcoming C# limitations
    ... This would have been useful since I could use this macro in any scope ... knowing that the destructor of MyObj will be called at ...
    (microsoft.public.dotnet.languages.csharp)