lua-users home
lua-l archive

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


> 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
> ...

Thats not 100% correct ;-)

first, you can also write
  var1 = "3"
  if tonumber(var1) < 4 then

But, there is a difference between multiplication and compare...
You can only multiply a numeric value, so it is IMHO ok to try to
convert to a number 'automagically'.
But you can also compare strings... so why dont you want to convert the
second operand in your example to be converted to a string instead?

  var1 = "3"
  if (var1 < "4") then

is absolute valid code.

so, there cannot be an automatic type conversion IMHO.

HTH,

   Torsten