[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: inconsistencies between arithmetic and boolean operators
- From: Adrien de Croy <adrien@...>
- Date: Fri, 10 Aug 2007 19:13:23 +1200
As work on __cast progresses, I've come across an issue relating to
boolean operators.
Arithmetic operators attempt to convert both operands to a number first,
so they work fine with __cast.
boolean operators insist however that the 2 operands have the same type,
else the result is zero. This is checked first.
This means that the following code
var1 = "3";
var2 = var1 * 2;
is valid and var2 will have the numeric value of 6
however
var1 = "3"
if(var1 < 4) then
...
is invalid, since var1 and 4 have different types, the result is an
error. If you wanted this to "work" you'd need to write
var1 = "3"
if((var1 * 1) < 4) then
...
the multiplication converts var1 in-place to a number.
Shouldn't the boolean operators try and do something like this?
e.g. instead of simply returning false on a type mismatch, try (e.g. by
__cast metamethod) to demote the higher type to the lower type, where
the precedence of types is (in descending order)
TABLE
USERVALUE
STRING
NUMBER
BOOLEAN
Adrien
--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com