tcl Procedure arguments The {*} syntax


Sometimes what you have is a list, but the command you want to pass the items in the list to demands to get each item as a separate argument. For instance: the winfo children command returns a list of windows, but the destroy command will only take a sequence of window name arguments.

set alpha [winfo children .]
# => .a .b .c
destroy $alpha
# (no response, no windows are destroyed)

The solution is to use the {*} syntax:

destroy {*}[winfo children .]


destroy {*}$alpha

What the {*} syntax does is to take the following value (no whitespace in between!) and splice the items in that value into the command line as if they were individual arguments.

If the following value is an empty list, nothing is spliced in:

puts [list a b {*}{} c d]
# => a b c d

If there are one or more items, they are inserted:

puts [list a b {*}{1 2 3} c d]
# => a b 1 2 3 c d