Nar*_*rek 3 tcl upvar proc-object
想从TCL专业人士那里获得最佳实践的建议.
假设您要使用proc构建包含特定数据的列表.现在哪种方式最好?
proc processList { myList } {
upvar $myList list_
#append necessary data into list_
}
proc returnList {} {
set list_ {}
#append necessary data into list_
return $list_
}
set list1 {}
processList list1
set list2 [returnList ]
Run Code Online (Sandbox Code Playgroud)
推荐哪种做法?
编辑:对不起,我无法理解回答这个问题的人的共识(和解释).
我几乎总是使用第二种方法:
proc returnList {} {
set result {}
# ... accumulate the result like this ...
lappend result a b c d e
return $result
}
set lst [returnList]
Run Code Online (Sandbox Code Playgroud)
内存使用率或速度几乎没有差异,但我发现在功能上思考更容易.此外,在Tcl 8.5中,您可以相对简单地分割结果列表(如果这是您需要的):
set remainderList [lassign [returnList] firstValue secondValue]
Run Code Online (Sandbox Code Playgroud)
就这样,你会最终a在$firstValue,b在secondValue,和c d e中$remainderList.