lua-users home
lua-l archive

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



I guess I'm trying to shield my users from having to consider type.

Lua already does a certain amount of this as described in the section of the lua documentation on lua types. E.g it automatically converts any type to a number for arithmetic operations, and types of variables are mutable.

I agree it's perfectly valid to compare strings, although the numeric representation of strings can have some unexpected consequences in a string compare, since it also takes into account length of string, and would make a mess of signs.

remember that all this is in the context of a __cast metamethod. the concept of a __cast operator is to allow automatic conversion of types where this is explicitly provided for by the host by registering the metamethod. If you don't register the __cast metamethod, you don't get automatic type conversion.

I coded it up quickly, and it now allows me to compare objects as strings.

my problem is I have string classes in C++ with methods that I want to expose to lua, yet also have lua to be able to treat them as a string. E.g I publish my string object to lua as a table. this way I can still call members on it, but if I want to use it as a string at all, it needs to be converted into a lua string.

I wrote a __cast metamethod to hook the tostring and tonumber calls, and this then allowed me to pass my table-representing-a-string into functions that wanted a string, but other operators didn't work, such as equality, lt, gt etc.

I could register metamethods for these operators as well, but if we are thinking about registered automatic type conversion, that should be able to do it as well, and saves a lot of subsequent metamethod lookups.

otherwise if I went with strict lua syntax etc, I'd have to get my users to write lua code such as

function filter(Session)

if(Session.UserBalance.AsNumber() < WinGate.Globals.MinBalance.AsNumber()) then
      return false;
   end;

   if(Session.Username.AsString() == "adrien")then
      return true;
   end

if(string.find(Session.Request.Header["User-Agent"].AsString(), "gecko"))then
      return true;
   end

-- block access return false;
end

rather than

function filter(Session)

   if(Session.UserBalance < WinGate.Globals.MinBalance) then
      return false;
   end;

   if(Session.Username == "adrien")then
      return true;
   end

   if(string.find(Session.Request.Header["User-Agent"], "gecko"))then
      return true;
   end

-- block access return false;
end

I guess it's not so bad really, but I want to use a consistent syntax for object access from Lua as I do from my own expression evaluation code in the host application, which doesn't need casting because it knows whether the object is being indexed or not already. The constant calls to AsString() and AsNumber() get a bit tedious. If someone leaves them out, they get a policy bug.

It seems that lua has so much scope for overriding behaviour with metamethods, that something like this shouldn't be so far out of whack?

Adrien





Torsten Karwoth wrote:
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

--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com