【发布时间】:2010-07-13 07:50:56
【问题描述】:
关于 Tcl 的纯理论问题。
在this question 之后,我正在考虑在 Tcl 中实现匿名函数的最佳方式。
最终结果应该是允许开发人员将完整的 proc 作为参数传递给另一个 proc:
do_something $data {proc {} {input} {
puts $input;
}};
类似于 javascript 的
do_something(data, function (input) {
alert(input);
});
现在,这自然不会在 OOTB 中起作用。我正在考虑这样的事情:
proc do_something {data anon_function} {
anon_run $anon_function $data
}
proc anon_run {proc args} {
set rand proc_[clock clicks];
set script [lreplace $proc 1 1 $rand];
uplevel 1 $script;
uplevel 1 [concat $rand $args];
uplevel 1 rename $rand {}; //delete the created proc
}
这行得通。但我希望得到更好的模式的建议,因为它不是很优雅,也没有真正使用很酷的 Tcl 特性。大多数情况下,我想摆脱手动调用anon_run。
【问题讨论】:
标签: tcl