Use of [list a b c] vs {a b c} when creating a list

tcl

Solution

The main difference is that using the `list` command makes it possible to use variables when defining the list. Notice the difference between these two:

% set foo 1
1
% set bar 2
2
% set list1 [list $foo $bar]
1 2
% set list2 {$foo $bar}
$foo $bar

Note that you can also use double quotes if you want:

% set list3 "$foo $bar"
1 2

It's important to note that of the two ways to build a list with variables, only using `list` is guaranteed to give you a proper list. Using quotes may or may not give you a list, depending on the contents of the variables. This isn't because Tcl is mysterious or random or buggy -- it's simply how tcl quoting works. With `list` you are asking tcl to construct a list with specific elements, in the others you're creating a string that looks like a list, but whether it can be treated like a list or not depends on the data in the string.

Here's an example where using quotes won't give you a list:

% set foo "{"
{
% set list4 "$foo $bar"
{ 2
% lindex $list4 0
unmatched open brace in list

... whereas using list will give you a proper list:

% set list5 [list $foo $bar]
\{ 2
% lindex $list5 0
{

It's important to know that the backslash appears only when tcl converts the list to a string for the purposes of printing the list -- the backslash isn't in the data, as you can see when you use `lindex` to fetch the value.

Problem

What differences are there between creating a list in TCL using: ``` [list a b c] ``` vs ``` {a b c} ``` I'm by all means not an experienced TCL programmer, but the only difference I have encountered so far is when creating a list of multiple lines the first style requires using line continuation characters like: ``` [list \ a \ b \ c \ ] ``` where this parses fine: ``` { a b c } ``` Are there any other differences? Which is considered better style or idiomatic? It would appear that when creating a complex list with nested lists, the 2nd style is the only clean way to go.

Original source