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.
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)
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.
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.
精彩评论