假设我有以下结构:
package require Itcl
itcl::class AAA {
private variable m_list {}
constructor {} {
fill m_list list
}
}
如何在 m_list 上获取参考以便编写
foreach elem $reference {.......}
考虑到这个列表真的很大,我不想复制它!
Tcl 变量使用写时复制语义。您可以安全地传递一个值,为其分配多个变量,而不必担心它会占用更多内存空间。
例如
set x {some list} ;# there is one copy of the list, one variable pointing at it
set y $x ;# there is one copy of the list, two variables pointing at it
set z $y ;# there is one copy of the list, three variables pointing at it
lappend z 123 ;# there are two copies of the list
;# x and y pointing at one
;# z pointing at the other
;# which is different from the first via an extra 123 at the end
上面的代码将产生两个巨大的列表,一个包含 x 任意 y 指向的原始数据,另一个包含仅 z 指向的额外元素 123。在 lappend 语句之前,列表只有一份副本,所有三个变量都指向它。
Here is how to get a reference on the member of a class:
package require Itcl
itcl::class AAA {
public variable m_var 5
public method getRef {} {
return [itcl::scope m_var]
}
}
AAA a
puts [a cget -m_var]
set [a getRef] 10
puts [a cget -m_var]