了解 Tcl 多态性
在 Tcl 中,变量和过程可以具有相同的名称...
例如我可以
set container(alist) {}
proc container a {puts " do something"}
嗯...tcl 中还存在哪些其他形式的多态性? ...我正在查看一些代码,我看到了这样的东西。
In Tcl a variable and a procs can have the same name ...
for instance I can have
set container(alist) {}
proc container a {puts " do something"}
Um ... what other forms of polymorphism exist in tcl? ... I am looking at some code and I see stuff like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这并不是真正的多态性。
变量,无论是普通变量还是数组,都可以与 proc 具有相同的名称,但 tcl 从上下文中知道哪个是哪个。查看
info
命令。特别是info procs
和info vars
。两者位于解释器内不同的名称空间中(顺便说一下,不要与 TCL
namespace
命令混淆)I don't think this is really polymorphism.
A variable, be it a plain variable or an array can have the same name as a
proc
, but tcl knows which is which from the context. Have a look at theinfo
command.info procs
andinfo vars
in particular.The two live in different name spaces within the interpreter (not to be confused with the TCL
namespace
command by the way)多态性是指一个对象能够被视为不同类型的对象并像另一种类型的对象一样使用。在您的示例中,您有一个同名的变量和过程,但它们不是,而且实际上不能被视为彼此(变量不能像过程一样被调用,过程不能被像变量一样对待)。
您也可能会争辩说,TCL 中不可能存在多态性。由于 TCL 将所有内容视为字符串(它是无类型语言),因此不存在“其他”数据类型。因此,您不能将类型 A 的对象视为类型 B 的对象,因为不存在类型 B。
您可以通过在不同的命名空间中定义具有相同名称的过程来为过程创建一种伪多态性。然而,这并不是多态性,而是运算符重载。
您可能想阅读这篇文章,了解TCLers Wiki。
Polymorphism refers to one object being able to be seen as and used like a different type of object. In your example, you have a variable and a proc with the same name but they are not, and indeed cannot, be treated as each other (the variable cannot be called like a proc and the proc cannot be treated like a variable).
You could also argue that there is no polymorphism possible in TCL. Since TCL treats everything as a string (it's a typeless language), there is no "other" data type. Thus, you can't treat an object of type A as if it were of type B because no type B exists.
You can create a sort of pseudo-polymorphism for procs by defining procs with the same name in different namespaces. However, that is not as much polymorphism as it is operator overloading.
You may want to read this article regarding polymorphism on The TCLers Wiki.
它本身不是多态性(名称只是以不同的方式查找),但命名的三个主要领域是命令(例如过程)、变量和< em>命名空间。还有其他一些(例如频道),但它们通常没有重叠的名称。
如果你有 8.6,“多态性”的另一个主要类别是方法名称。
It's not polymorphism as such (the names are just looked up in different ways), but the three main areas of naming are commands (e.g., procedures), variables, and namespaces. There are a few other ones too (such as channels) but they usually don't have overlapping names anyway.
If you have 8.6, another major class of “polymorphism” is method names.