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.

Regards,

ant

Hi,

If you want objects with a local scope, you'll be happy to know
that there is an itcl::local command.

proc mytest {} {
itcl::local SomeClass obj
[....
use obj
....]
return ...;# obj is automatically deleted at proc's return
}

Link : http://www.tcl.tk/man/itcl3.1/local.n.html

Sincerly yours,

Stéphane A.
.



Relevant Pages

  • Incr Tcl: Something like a scoped object
    ... Does Incr Tcl offer something like scoped variables in C++ or context ... scope, automatically has a certain finalization method invoked on it? ... puts "Destructor called." ... # myScopedObject goes out of scope ... ...
    (comp.lang.tcl)
  • Re: Why do I have to call del explicitly for com objects?
    ... destructor concerned with obj unless the sleep is commented out. ... del does not invoke a destructor, just decrements the object's reference ...
    (comp.lang.python)
  • Re: Why do I have to call del explicitly for com objects?
    ... destructor concerned with obj unless the sleep is commented out. ... That is, "some time in the future", the GC would destroy it (unless it's part of a circular reference chain...) ... I can force the destructor to be called using del obj at the end ...
    (comp.lang.python)
  • Re: Thread cancellation
    ... Thread is not knowing anything about Object/Constructor or Destructor. ... obj will be called or not? ... when we cancel the thread executing somefunction, ...
    (comp.unix.programmer)
  • Re: Thread cancellation
    ... Constructor of 'class a' takes mutex, destructor of 'class a' releases ... Above somefunction is executed by thread. ... obj will be called or not? ...
    (comp.unix.programmer)