There is no notion of local procedure inside a procedure
Closed / Wont Fix9ca25bce1341913e910f612f13a64ac9cfea2598Created 12 years agoUpdated 12 years ago
Descriptionplain2015-04-18 02:57:58
Hi, when objects are created inside a procedure using TclOO they are global to all the current namespace. They are not deleted when the function returns. The problem is that these objects are commands. And all commands, including the ones that are created inside another procedures, are global to all the current namespace. The situation is totally different with variables. When a variable is defined inside a function, this variable is local to this function. When the function returns, this variable does not exists. But inside a procedure, this variable can be global to all the namespace using the global command. I think that this behaviour should be equal with procs. All the procs defined inside another proc should be local, unless they are indicated as globals with the global command. Otherwise each time a user define objects using TclOO inside a function they will not be deleted automatically when the function returns. Another possible approach is to create all the objects inside a specific namespace, and delete this namespace before the function returns. But the automatic deletion of local procedures defined inside another procedure, I think that it's a better solution because the user doesn't need to worry about to create a special namespace and delete this namespace before the functions returns. Another choice is to have a control of all the created objects using a static variable in the class. And before the function returns call a specific static method of the class to remove all the created objects. Again, the drawback with this approach is that the user needs to worry about to make a clean-up before the function returns calling this method.
anonymousplain2015-04-18 05:11:43
My new approach is this:
Each variable or procedure created in the ::local namespace is automatically deleted when a function returns.
Inspired by this code:
http://wiki.tcl.tk/997
proc local_procs {} {
namespace eval ::local {}
uplevel 1 {
set __[lindex [info level 0] 0] {}
trace variable __[lindex [info level 0] 0] u {namespace delete ::local;}
}
}
With this new approach, I have to write local_procs at the begining of each proc. But it would be nice if this happens automatically by the interpreter.
dkfhtml2015-05-16 06:11:07
They are not deleted when the function returns.This is correct, and there's a lot of existing code that relies on exactly this behaviour (so one can have procedures that create other permanent procedures) so it definitely won't be changing in the way that you ask for.
If you want procedure-like things that will be automatically deleted at the end, use lambda terms. That works well and they are almost as simple to use as procedures (just apply $term instead of the procedure name):
proc foo {r s t} {
set bar {{x y} {
set z [expr {$x**2 + $y**3}]
return [expr {17 * $z + 42}]
}}
set z "example $t"
set abc [apply $bar $r $s]; #### <<<<<<<<<<<<<
return [list $z $abc $z]
}
Log in (anonymous is fine) to add a comment.