Tcl Source Code

View Ticket
Login
Ticket UUID: 818100
Title: not all equal booleans are equivalent
Type: RFE Created on: 2003-10-05 13:13:16
Submitter: nobody Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2005-11-02 00:17:45
Status: Closed Closed by: dgp
Resolution: Out of Date Closed on: 2005-11-01 17:17:45
Version: None
Description:
% expr {(0 == false) ? [puts true] : [puts "you're
screwed"]}
you're screwed

miguelGeorge: let me check for an explanation, 5'
miguelYep, seems to be a bug: the code of INST_EQ does
not attempt a boolean conversion, it compares as
numbers or strings only.
User Comments:
dkf added on 2003-10-06 06:07:51:
Logged In: YES 
user_id=79902

It's not clear to me whether 0 and false should be equal in
the context of the == operator.  Indeed, == is defined (as
are many other [expr] operators) to operate on
preferentially numbers, and otherwise strings.  In terms of
that, "false" is not numeric, and "0" is a clearly different
string.

IMO it's not a bug.  Just a misreading of the manual by the
person submitting.

mistachkin added on 2003-10-06 06:32:14:
Logged In: YES 
user_id=113501

In my opinion, we need a TIP to add a math func "bool()" 
which attempts to perform boolean conversion on it's 
argument.

bool(true) == 1
bool(false) == 0
bool(yes) == 1
bool(no) == 0
bool(<any-non-zero-value>) == 1
bool(<any-zero-value>) == 0

This would make expr more complete.

dkf added on 2003-10-06 20:25:44:
Logged In: YES 
user_id=79902

If you (or anyone else) want to write the TIP, feel free.

I'd be tempted to make anything not recognized by
Tcl_GetBooleanFromObj() an error when fed to bool(), just as
int("some string") is illegal too.

dkf added on 2003-11-13 07:08:11:
Logged In: YES 
user_id=79902

I'm not going to do anything about this on my own; the
current behaviour is exactly as documented and not a bug. 
It's up to someone else as far as I'm concerned.

A workaround is to use !!, like this:
  expr {(!!0 == !!false) ? [puts Cool!] : [puts D'oh!]}

dgp added on 2005-11-02 00:17:45:
Logged In: YES 
user_id=80530


TIP 182 resolved this issue.