TIP 515: Level Value Reform

Author:         Jan Nijtmans <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        7-Sept-2018
Tcl-Version:	8.7
Vote-Results:   8/0/1 accepted
Votes-For:      DKF, KBK, JN, JD, DGP, FV, SL, AK
Votes-Against:  none
Votes-Present:  BG
Tcl-Branch:     tip-515


Proposes reformed handling of Tcl level values. This TIP is inspired by TIP #502, which handles "index" values, realizing that "level" values have kind of the same problems as described there.


Many Tcl programmers may be surprised by these results

% apply {{} {uplevel -4294967294 {puts OK}}}
bad level "-4294967294"  (OK, this is expected ....)
% apply {{} {uplevel -4294967295 {puts OK}}}
% apply {{} {uplevel -4294967296 {puts OK}}}
invalid command name "-4294967296"

I would expect the same "bad level" error in all 3 cases. Or:

% proc todo arg {puts $arg}
% apply {{} {uplevel todo OK}}
% proc 2do arg {puts $arg}
% apply {{} {uplevel 2do OK}}
bad level "2do"

Why can't we call a command "2do"? The parser appears to be confused because the command name starts with a digit.


Revise the parsing of values used as level values to accept all integer representations acceptable to expr, optionally preceded by "#".

Although this suggests an unlimited range of valid levels, in practice all out-of-range levels (either a negative number, either a number higher than the actual available number of levels) , will result in a "bad level" error.


Examples like the ones in the Background above will incompatibly change from one outcome to a different outcome. This is a true incompatibility, but it is difficult to believe anyone actually desires the outlier behaviors illustrated above, much less has code that relies on them.

New behavior, one that succeeded in Tcl 8.6 but should actually fail:

% apply {{} {uplevel -4294967295 {puts OK}}}
bad level "-4294967295"

Another example, of a situation that failed in Tcl 8.6 but should succeed:

$ proc 2do arg {puts $arg}
% apply {{} {uplevel 2do OK}}


Currently, the proposed implementation is available in the tip-515 branch.


This document has been placed in the public domain.