TIP #184: Avoid Creating Unusable Variables


TIP:184
Title:Avoid Creating Unusable Variables
Version:$Revision: 1.4 $
Author:Miguel Sofer <msofer at users dot sf dot net>
State:Final
Type:Project
Tcl-Version:8.5
Vote:Done
Created:Saturday, 27 March 2004
Keywords:Tcl, upvar, global

Abstract

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.

Rationale

As detailed in [Bug #600812] [1], 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 [2] that implements this TIP.

Copyright

This TIP is in the public domain.


Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows