TIP 772: Tuple Assignment 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-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

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

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

  3. Extra parentheses around the target are allowed: ((a, b)) = $v. One name in parentheses is a plain assignment: (x) = {1 2} sets x to 1 2.

  4. A value that is not a list is an error (unmatched open brace in list).

  5. 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, 2 would read ((a, b) = 1), 2: a set to 1, b silently 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 code TCL PARSE EXPR TUPLE). The value is written (a, b) = (1, 2); (((a, b) = $v), 3) and f((a, b) = $v) (the only argument) remain valid, as do scalar assignments among elements: (x = 1, y = 2).

  6. 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:

This document has been placed in the public domain.