lua-users home
lua-l archive

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

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


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)



Adrien de Croy - WinGate Proxy Server -