lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



actually you'd need to put STRING below NUMBER, else

1 > "Fish"

if we converted the rhs to a number would return true, as "Fish" would convert to 0. whereas

1 > "Fish"

if we converted the lhs to a string would return false on the string compare since "1" < "Fish" using lstrcmp.

In the end, probably best to just convert anything to a string if they aren't the same type then do the comparison.

e.g.


int lua_CheckTypes(lua_State* L, TValue *lhs, TValue *rhs)
{
   int nLhsType = ttype(lhs);
   int nRhsType = ttype(rhs);
   const TValue* tmL;
   const TValue* tmR;
   if(nLhsType == nRhsType)
      return 1;

   // just do all comparisons as string.
   return (tostring(L, lhs) && tostring(L, rhs));
}


then in code where we call

   if(ttype(lhs) == ttype(rhs))

we replace that with

   if(lua_CheckTypes(L, lhs, rhs))



Adrien


Adrien de Croy wrote:

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