This is not necessarily the current version of this TIP.
| TIP: | 182 |
| Title: | Add [expr bool] Math Function |
| Version: | $Revision: 1.10 $ |
| Authors: |
Joe Mistachkin <joe at mistachkin dot com> Don Porter <dgp at users dot sf dot net> |
| State: | Draft |
| Type: | Project |
| Tcl-Version: | 8.5 |
| Vote: | Pending |
| Created: | Tuesday, 23 March 2004 |
This TIP proposes a new expr math function bool().
Several of the Tcl/Tk built-in commands make use of the notion of a Boolean value, a value that can be either true or false. Boolean values show up in control commands like if and while, and they also are used to enable/disable certain features, as in tcltest::singleProcess $bool and namespace ensemble configure $ens -prefixes $bool.
There have long been two distinct notions of the set of string values that are recognized as boolean values.
The Tcl_GetBoolean() routine recognizes the following strings, in all case variations, and nothing else as valid boolean values: 0, 1, yes, no, true, false, on, off. Several script-level commands are implemented by calls to Tcl_GetBoolean, and also recognize only this limited set of string values as boolean values. Examples include string is boolean, string is true, string is false, and fconfigure $chan -blocking. Examples from Tk include configuration options of the canvas, text, and scrollbar that expect boolean values.
The Tcl_ExprBoolean() routine interprets the result of an expression evaluation as a boolean. It recognizes all the values recognized by Tcl_GetBoolean(), but it also recognizes all numeric values as booleans as well. Any string which Tcl can interpret as any kind of number is a boolean according to Tcl_ExprBoolean() with the non-zero numbers seen as true, and others seen as false. Script-level commands such as if and while have adopted this view of booleans, so a script like while {[incr x -1]} {} works as expected. It also means the script if 0x1 {} is perfectly acceptable, even though string is boolean 0x1 reports that 0x1 is not a boolean.
The Tcl_GetBooleanFromObj() routine arrived in Tcl 8.0, and contrary to what its name might suggest, it was and is not an Obj-ified form of Tcl_GetBoolean. Instead, Tcl_GetBooleanFromObj() adopted the Tcl_ExprBoolean understanding of what was and was not a boolean value. Over time as more Tcl and Tk commands were Obj-ified, it was natural to replace Tcl_GetBoolean calls with Tcl_GetBooleanFromObj and in the process several commands have come to accept a broader class of boolean values than they once did. For example,
% info patch 7.6p2 % clock format 0 -gmt 0x1 expected boolean value but got "0x1"
% info patch 8.0.5 % clock format 0 -gmt 0x1 Thu Jan 01 00:00:00 GMT 1970
Another (accidental?) change in behavior in Tcl_LinkVar() sneaked in with the TIP #72 changes between Tcl 8.3 and Tcl 8.4. A linked variable of type TCL_LINK_BOOLEAN now allows the Tcl variable to hold any numeric value, while in pre-8.4 releases of Tcl, only those values acceptable to Tcl_GetBoolean were permitted.
Tcl's built-in math functions include three that are devoted to "casting" operations, int(), double(), and wide(). In each case, t