Version 20 of {*}

Updated 2007-07-19 17:27:17 by LV

The artist formerly known as {expand}. See TIP 293 [L1 ] for details.

Rather than chase around to all these other pages, let's document here just what the new syntax in Tcl 8.5 does.

In older versions of Tcl, one would write:

 eval destroy [winfo children .]
 eval button .b $stdargs -text \$mytext -bd $border
 eval exec \$prog $opts1 [getMoreopts] \$file1 \$file2

   The eval version would be even more complex if the lists that are to be
   expanded are not known to be pure. To be really safe the last would be:

 eval exec \$prog [lrange $opts1 0 end] [lrange [getMoreopts] 0 end] \$file1 \$file2     

With the new syntax the examples become:

 destroy {*}[winfo children .]
 button .b {*}$stdargs -text $mytext -bd $border
 exec $prog {*}$opts1 {*}[getMoreopts] $file1 $file2


RLH This is much better IMHO.

jcw - HURRAY!

RS Note that in re_syntax, * stands for "zero or more", which is exactly the unique cardinality of an {expand}ed item...

DKF: Similarly in string match syntax.


SYStems I vote yes. I don't think though that its necessary to deprecate {expand}, but I agree on deprecating {} it does look like syntax error rather than a feature.

RLH I think {expand} should be removed completely. It is not in the language "official" yet and so it should be yanked to make sure no abuse occurs.

JMN Well.. {*} looks to me like a nipple.. which is clearly better than a wart.


CN slightly regrets that {expand} is now gone - I was looking forward for possible future enhancements along the same line, like for example

   lappend mylist {sort|toupper|regmatch [0-9]+}$somelist

to specify a chain of filters for the expansion. But I suppose that people for whom {expand} is a wart will classify this a as a tumor... ;-)

DKF: It's also trivially replacable using {*}[...] and some commands to preprocess the list.


SYStems or you can just

   lappend mylist {*}[lsort [string toupper [regmatch {[0-9]+} $somelist]]]

moral is {*} is not a shorthand for other commands, its a new feature, although for this particular example you could have just used

 set mylist [concat $mylist [lsort [string toupper [regmatch {[0-9]+} $somelist]]] ]

AMG: When I have a long command but don't feel like using \ at the end of every line, I sometimes do the following:

 set data [dict create {*}{
     key1 val1    key2 val2
     key3 val3    key4 val4
     key5 val5    key6 val6
 }]

Of course, in this case I could have just left out the [dict create] and instead relied on the internal representation being generated the first time I tried accessing $data as a dict. But the above is slightly faster and clearer... well, except for the {*} nonsense, of course. :^) Given a bit of time, I'm sure I could come up with a more realistic example, but I actually find myself doing the above in real code (if anything I write can be said to be "real").

This usage of {*} provides quoting without grouping. Dollar signs and square brackets become ordinary characters, sequences of unquoted whitespace and newlines are delimiters, and the resulting list elements (all of which are pure strings) are placed one-after-the-other into the command's objv list without any extra interpretation. Backslashes, paired braces, and paired double quotes still function as quoting/grouping characters. ("Grouping" means to disable the special meaning of characters that would otherwise be treated as delimiters.)


For the impatient, see Expansion with {*} in Tcl 8.4


[ Category Syntax | Dodekalogue ]