TIP 184: Avoid Creating Unusable Variables

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.
Author:		Miguel Sofer <[email protected]>
State:		Final
Type:		Project
Vote:		Done
Created:	27-Mar-2004
Keywords:	Tcl, upvar, global
Tcl-Version:	8.5
Tcl-Ticket:	600812


Both upvar and global can create unreachable variables: scalar variables whose name looks like an array element, e.g., a(b). This behaviour is documented in the upvar manpage. This TIP proposes that both upvar and global raise errors instead of creating such variables.


As detailed in [Bug #600812] http://sf.net/tracker/?func=detail&aid=600812&group_id=10894&atid=110894 , both upvar and global can create unreachable variables: scalar variables whose names looks like an array element.

One example is:

   upvar 0 x(1) y(1)

which creates a scalar variable named y(1) that is linked to the element 1 of the array x. However, there is no way for a tcl script to read or write such scalar variables, the parser will interpret that name as referring to an element of the array named y.

Another example is:

   proc a {} {
       global x(1)

which will create a scalar local variable named 'x(1)', linked to the element 1 of theglobal array x. Again, this variable is unreachable.

This TIP proposes upvar and global raise an error rather than creating such a variable, mimicking in this respect the behaviour of variable.

Note that a TIP is required because the behaviour of upvar is documented in the manual page, so that it cannot really be described as a bug:

MyVar is always treated as the name of a variable, not an array element. Even if the name looks like an array element, such as a(b), a regular variable is created.

Reference Implementation

There is a patch attached to the bug ticket http://sf.net/tracker/?func=detail&aid=600812&group_id=10894&atid=110894 that implements this TIP.


This TIP is in the public domain.