了解 Tcl 多态性

发布于 2024-08-19 02:06:28 字数 193 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-08-26 02:06:28

我认为这并不是真正的多态性。

变量,无论是普通变量还是数组,都可以与 proc 具有相同的名称,但 tcl 从上下文中知道哪个是哪个。查看 info 命令。特别是 info procsinfo 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 the info command. info procs and info 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)

甜柠檬 2024-08-26 02:06:28

多态性是指一个对象能够被视为不同类型的对象并像另一种类型的对象一样使用。在您的示例中,您有一个同名的变量和过程,但它们不是,而且实际上不能被视为彼此(变量不能像过程一样被调用,过程不能被像变量一样对待)。

您也可能会争辩说,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.

太阳公公是暖光 2024-08-26 02:06:28

它本身不是多态性(名称只是以不同的方式查找),但命名的三个主要领域是命令(例如过程)、变量和< 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文