哪种方式更好?变量相关 [英] Which is better way? Variables related

查看:40
本文介绍了哪种方式更好?变量相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好:

variable var1 $current_file_generation_info::language

set var1 $current_file_generation_info::language

<小时>

另外,哪个更好:


Also, which is better way:

variable current_file_generation_info::language $var1

set current_file_generation_info::language $var1

推荐答案

如果在 namespace eval 中,请使用 variable 以避免奇怪变量名解析器.(我不想详细描述它的作用,只是说依赖它的代码可以自动描述为有缺陷的,并且很难准确描述.)

If in a namespace eval, use variable to avoid the weirdness of the variable name resolver. (I don't want to describe in detail what it does, other than to say that code that relies on it can be automatically described as buggy and in hard to accurately describe ways.)

如果在一个过程中,(通常)使用 set 除非你想要一个耦合的局部变量(与命名空间变量的最后一个组件同名)到局部变量,因此对一个的任何操作都是对另一个的操作.例如,如果您这样做:

If in a procedure, (usually) use set unless you want a local variable (with the same name as the last component of the namespace variable) that is coupled to the local variable so any manipulation of one is a manipulation of the other. For example, if you do:

variable foobar::grill "sprocket"

局部变量 grill 将被创建并绑定到命名空间 foobar 中的命名空间变量 grill(相对于当前命名空间或到全局命名空间;这是命名空间解析",它比上面提到的变量解析简单!).命名空间变量 grill,如果它不存在,将被特别标记为存在(但最初未设置).最后,因为我们确实提供了一个值 (sprocket),该值将存储到命名空间变量中(因此也会出现在局部变量中).

The local variable grill will be created and bound to the namespace variable grill in the namespace foobar (either relative to the current namespace or to the global namespace; this is "namespace resolution" and it is simpler than variable resolution, mentioned above!). The namespace variable grill, if it didn't already exist, will be specially flagged as existing (but initially unset). Finally, because we did supply a value (sprocket) that will be stored into the namespace variable (and hence also appear in the local variable).

如果您要在一个过程中多次读取或写入命名空间变量,将其绑定到局部变量并对其进行处理会快得多.一旦绑定,您就无法真正解除绑定,但这通常不是问题.(请注意,当您使用 upvarglobalnamespace upvar 时,绑定过程实际上是相同的,尽管每种方法都有不同的方法来查找什么它绑定到.)

If you are going to read or write a namespace variable multiple times in a procedure, it is considerably faster to bind it to a local variable and work on that instead. Once bound, you can't really unbind them, but it is usually not a problem. (Note that the binding process is effectively the same when you use upvar or global or namespace upvar, though each has different ways to look up what it is binding to.)

如果您这样做是为了模拟类,请考虑使用真实的类.有几个选项(incr Tcl、XOTcl、TclOO、Snit).

这篇关于哪种方式更好?变量相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆