Author: Florent Merlet <[email protected]>
State: Draft
Type: Project
Vote: Pending
Created: 29-Oct-2026
Tcl-Version: 9.2
Tcl-Branch: tip-759e-tcl91-tuple-assign
Keywords: expr, assignment, list, lassign
Post-History:
Abstract
In an expression, the target of the assignment operator of TIP 282 may be a list of names, nested at will:
((x0, y0), (x1, y1)) = ($p, $q)
Each variable gets the element of the value at the same place. It is the
fifth part of TIP 759; it needs TIP 282 (=) and the lists of TIP 759
((a, b)).
Rationale
lassign destructures one level. Geometry, matrices and records are
nested lists: unpacking two points takes two lassign and four names, or
lindex calls. A tuple target is the mirror of the list expression that
builds the value, so the code reads the same in both directions:
proc distance {p q} {(
((x0, y0), (x1, y1)) = ($p, $q);
hypot($x1 - $x0, $y1 - $y0)
)}
Specification
The target is a list as in TIP 759 lists: names separated by commas, in parentheses, nested at will. As for
=, a name is a bareword (not followed by(), or any other operand whose value is the name:{::ns::v},"A($i)",$ref,f(x).Each variable is set to the element of the value at the same path, as given by
lindex: a missing element gives the empty string, extra elements are ignored. The value of the assignment is the value.Extra parentheses around the target are allowed:
((a, b)) = $v. One name in parentheses is a plain assignment:(x) = {1 2}setsxto1 2.A value that is not a list is an error (
unmatched open brace in list).Not among other elements or arguments. A tuple assignment may not be an element of a list or an argument of a function among others, unless it is enclosed in parentheses. Since a comma outside of any parentheses makes the whole expression a list (lists TIP), and the comma binds looser than
=,(a, b) = 1, 2would read((a, b) = 1), 2:aset to 1,bsilently set to the empty string. It is an error instead (a tuple assignment is not allowed between the arguments of a function or the elements of a list: use parentheses, as in (a, b) = (1, 2), error codeTCL PARSE EXPR TUPLE). The value is written(a, b) = (1, 2);(((a, b) = $v), 3)andf((a, b) = $v)(the only argument) remain valid, as do scalar assignments among elements:(x = 1, y = 2).Bytecode. A new instruction
tupleAssign(stack:names value paths => value). The paths, one list{LOCAL i0 i1 ...}per name, are a literal. In a procedure, when every name is a compiled local, the variables are set by index and the list of names is not built at run time.
Future work
With variables declared to expr (see the note on TIP 282), array
elements could be written (A(0), A(1)) = $v without quotes.
Compatibility
Only expressions that are errors today change meaning.
Implementation
Patch 0005-TIP-759-5-7-tuple-assignment-a-b-c-list.patch:
tclCompExpr.c:FindTupleTarget,TupleRoot,TuplePlacesValid,TupleNamesOnly,TupleLocalNames,CollectTuplePaths.tclCompile.h,tclCompile.c:INST_TUPLE_ASSIGN.tclExecute.c:TupleIndexPath,TupleAssign.- Tests:
tests/exprTuple.test;exprAssign-5.6. Documentation:expr.n.
Copyright
This document has been placed in the public domain.
