TIP 577: Strict index values

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <[email protected]>
State:          Draft
Type:           Project
Created:        8-June-2020
Post-History:   
Keywords:       Tcl index
Tcl-Version:    8.7
Tcl-Branch:     strict-index

Abstract

Various commands handle errors in "index" values differently. This TIP attempts to treat the different uses of index values (in both Tcl and Tk) to a more uniform way.

Rationale

Some examples (Tk):

$ wish8.6
% listbox .l
% menu .m
% .l index 0
0
% .l index none
bad listbox index "none": must be active, anchor, end, @x,y, or a number
% .m index none
none
% .m index 0
none
% .m index foo
bad menu entry index "foo"

More examples (Tcl):

$ tclsh8.6
% lreplace {a b c} -1 -1 d
d a b c
% lreplace {a b c} -2 -2 d
d a b c
% set x {a b c}
a b c
% lset x 0 d
d b c
% lset x -1 d
list index out of range

Specification

Compatibility

For Tcl 8.7 this is almost 100% compatible, previous error-situations become valid now: Index value none can now be used in place of -1, just as the empty string "".

Since, starting with Tcl 9.0, some index forms are no longer valid, this is a potential incompatibility. Indices like -2 and end+2 can no longer be used in scripts: they will immediately result in an "index out of range" error, in stead of being silently ignore.

Implementation

See the strict-index branch.

Copyright

This document has been placed in the public domain.