not all equal booleans are equivalent
Closed / Out of Date818100ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 21 years ago
Descriptionplain2003-10-05 13:13:16
% 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.
dkfplain2003-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.
dkf Ticket [818100ffff]: 2 changes
[History diff]2003-10-06 06:07:52
mistachkin Pending ticket [818100ffff].
[History diff]2003-10-06 06:32:13
mistachkinplain2003-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 Open ticket [818100ffff].
[History diff]2003-10-06 20:25:38
dkf Ticket [818100ffff]: 3 changes
[History diff]2003-10-06 20:25:41
dkf Ticket [818100ffff]: 1 change
[History diff]2003-10-06 20:25:42
dkf Ticket [818100ffff]: 1 change
[History diff]2003-10-06 20:25:43
dkfplain2003-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 Ticket [818100ffff]: 1 change
[History diff]2003-10-06 20:30:20
dgp Ticket [818100ffff]: 1 change
[History diff]2003-11-13 05:51:11
dkf Ticket [818100ffff]: 1 change
[History diff]2003-11-13 07:08:10
dkfplain2003-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 Ticket [818100ffff]: 1 change
[History diff]2003-11-13 07:21:45
dgp Ticket [818100ffff]: 1 change
[History diff]2004-12-10 00:53:06
dgp Closed ticket [818100ffff].
[History diff]2005-11-02 00:17:44
dgpplain2005-11-02 00:17:45
Logged In: YES user_id=80530 TIP 182 resolved this issue.
Log in (anonymous is fine) to add a comment.