Reputation: 3
I'm a beginner who just recently learned some TCL and recently came across a puzzling TCL internal process that I cannot explain. I have the code below:
proc print_items{args} {
foreach item $args {
puts $item
}
}
set arr "A B C D"
print_items $arr
I was expecting the result to be:
A
B
C
D
but it was:
A B C D
Upon closer inspection. I noticed that $args was not the flat list I thought but instead was
> puts $args
{A B C D}
> llength $args
1
That means $args itself must have been wrapped in another list i.e $args must've been like this when received by the procedure: {{A B C D}}
So my question is: Why does TCL wrapped an already formed list in another list when passed inside print_items? This is very different from other programming languages that I've done. In which circumstances does the argument gets wrapped in another data structure (i.e: in another list, dictionary, array, etc.)?
Upvotes: 0
Views: 126
Reputation: 682
The keyword "args" in procedure arguments has a special meaning. It collects all parameters to a tcl list. For example:
proc foo { param1 param2 args } {
# here:
# $param1 = alice
# $param2 = bob
# $args = {alice1 bob1 alice2 bob2}
}
foo alice bob alice1 bob1 alice2 bob2
Thus, the answer for your question is: don't use the special word "args" as a prodecure argument because it is processes in a special way. Try to use any other name for procedure agrument.
Upvotes: 3