Twylite | 23 Jun 22:15

Re: Garbage collected handles / on_leave_frame / finally

Hi,
I'm only vaguely following this conversation so please disregard if OT.
> From: "Alexandre Ferrieux" <alexandre.ferrieux@...>
>       proc foo {} {
>           set x [somecreator]
>           on_leave_frame 0 [list destroy $x]
>       }
>   
I like it, but for a different reason :)  I've been mulling a TIP for a 
"finally" command that is not part of a try/finally construct.  Such a 
command would allow one or more "finally scripts" to be associated with 
a scope and evaluated when execution leaves the scope/frame.  The 
benefits are a simplification of error handling and support for freeing 
resources that are used within the scope.  I wasn't going as far as 
indicating the stack level / frame to which to attach the script though ;)

The simplification comes from less noise.  Consider simple file-parsing 
code:

Approach #A.1 - Leaks file handles
set f [open $myfile r]
# ... process data in $f ... don't use return here
close $f

Approach #A.2 - Tcl catch
set f [open $myfile r]
set failed [catch {
  # ... process data in $f ... don't use return here
} em opts]
close $f ;# finally
(Continue reading)


Gmane