1

I want to make several objects, all with the same parameters, so I tried to store them in a proc that returns them. But the interpreter evaluates the returning result as one parameter, instead of several. my proc is:

proc element_param {} {
    return "-filled 1\
        -visible 1\
        -linewidth 1\
        -linecolor yellow\
        -fillcolor yellow\
        -relief roundraised\
        -linewidth 2"
}

and I use it with:

$this/zinc add rectangle 1 [list "100" "100" "200" "200"] [element_param]

How do I turn them into several different parameters?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
SIMEL
  • 8,745
  • 28
  • 84
  • 130

1 Answers1

7

With tcl 8.5 and above use the {*} operator to expand the list of parameters:

$this/zinc add rectangle 1 $coords {*}[element_param]

with previous versions you can expand lists using eval:

eval [linsert [element_param] 0 $this/zinc add rectangle 1 $coords]

which is equivalent.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Or: `eval [list $this/zinc add rectangle 1 $coords] [element_param]` since it is a list being returned, even if it is built in a horrible way. – Donal Fellows Oct 19 '11 at 06:16
  • @Donal Fellows, why is it built in a horrible way, and what is a better way to build it? – SIMEL Oct 21 '11 at 06:39
  • 1
    @Ilya: It's better to use the `list` command, since that does the right thing with quoting if you decide you need options with a space in (for example). While it is possible to quote those things by hand, this is **strongly discouraged** because it's easy to get wrong; use `list` (or one of the other commands that makes a list) and have it done correctly for you, automatically. – Donal Fellows Oct 21 '11 at 08:10