Author: Florent Merlet <[email protected]>
State: Draft
Type: Project
Vote: Pending
Created: 29-Oct-2026
Tcl-Version: 9.2
Tcl-Branch: tip-759c-tcl91-list
Keywords: expr, list
Post-History:
Abstract
In an expression, two or more expressions separated by commas, in parentheses that do not follow a function name, give the list of their values:
expr {($x, $y)} ;# {3 4}
lmap a $L0 b $L1 {expr {($a, $b)}} ;# pairs
lmap x $xs {expr {($x, sin($x))}} ;# coordinates for a canvas
expr {$x, $y} ;# {3 4}: comma outside parens
set M [(1,2,3), (4,5,6), (7,8,9)] ;# a matrix
It is the third part of TIP 759. It does not depend on the other parts;
with the expression scripts of TIP 759 the loop bodies above become
{($a, $b)}.
Rationale
Building a list of computed values needs today [list [expr {...}]
[expr {...}]]. The comma is already the separator of function arguments,
and a comma between parentheses is a syntax error today, so the form is
free and reads naturally. Lists nest: ((1, 2), 3) is {1 2} 3.
Specification
In an expression, an open parenthesis that does not follow a function name, whose content has at least one comma at its top level, is a list: the value is the list of the values of the comma-separated expressions, as built by
list.A list is not converted to a number at the end of the expression.
Parentheses without a comma are unchanged:
(1+2)*3is still 9, and still folded to a constant by the compiler.A comma outside of any parentheses makes the whole expression a list, as if it were enclosed in parentheses:
1, 2is1 2,(1),2is1 2,(1,2),3is{1 2} 3. With an expression substitution, the parentheses close early, as in[(1)+(2)]:[(1,2),(3,4)]is the expression(1,2),(3,4),{1 2} {3 4}, two characters shorter than[((1,2),(3,4))].()and(1,)are errors, as today; so is1,.C API.
Tcl_ParseExprshows a list as a function call whose operator token is empty:(1,2)givessubexpr (1,2) 5 operator {} 0 subexpr 1 ... subexpr 2 .... A list outside of parentheses is shown the same way, its subexpression being the whole expression.
Compatibility
Only expressions that are errors today change meaning. Tests
parseExpr-21.30 to 21.32 (expr 0,0 and expr (0,0) were errors) are
updated.
Implementation
Patch 0003-TIP-759-3-7-lists-a-b-.-in-expressions.patch:
tclCompExpr.c: aNULL_FUNCnode is made before each open paren that is not a function's; a comma at the top of the parens turns it into aLISTnode, compiled toINST_LIST.NULL_FUNCcompiles to nothing and keeps constant folding. A comma that reaches theSTARTnode, outside of any parentheses, turns it intoSTART_LIST: the tree keeps its shape, no node is added, the end of the expression closes the list as it closes any expression;START_LISTcompiles toINST_LISTinstead oftryCvtToNumeric.ConvertTreeToTokenshandles all three.- Tests:
tests/exprList.test;parseExpr-21.30to21.32. Documentation:expr.n.
Copyright
This document has been placed in the public domain.
