TIP 770: Lists (a, b, ...) in Expressions

Login
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

  1. 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.

  2. A list is not converted to a number at the end of the expression.

  3. Parentheses without a comma are unchanged: (1+2)*3 is still 9, and still folded to a constant by the compiler.

  4. A comma outside of any parentheses makes the whole expression a list, as if it were enclosed in parentheses: 1, 2 is 1 2, (1),2 is 1 2, (1,2),3 is {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))].

  5. () and (1,) are errors, as today; so is 1,.

  6. C API. Tcl_ParseExpr shows a list as a function call whose operator token is empty: (1,2) gives subexpr (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:

This document has been placed in the public domain.