lua-users home
lua-l archive

Re: Comparing userdata with other types

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


My metamethods only have a special behavior if the number is a very specific NAN value. There are billions of NANs, so the chance of this value occurring normally is negligible. And the special behavior is limited to __index, __newindex and __len, which should not be used in normal numeric operations anyway (aside from __eq, if I get it to work somehow).

delbu9c1@erau.edu wrote: [From: Ivo Beltchev
Date: 2009年5月31日 15:05:19 -0700
To: Lua list
<lua@bazar2.conectiva.com.br>
Subject: Re: Comparing userdata with other types
I had some luck with using a NAN value with a metatable for numbers. So now I have:
NIL+x -> NIL
NIL[x] -> NIL
NIL[x]=5 -> no error
#NIL -> 0
NIL>20 -> false

But I'm still having 2 problems:

1) NIL==NIL gives false. That's because the __eq metamethod is not being called for numbers at all, and normally NAN!=NAN. Is there a trick to get this to work? It is important because otherwise there is no convenient way to detect when the value is really NIL. Basically I want to do:
if (obj.mass==NIL) ...

2) Since I'm replacing the __index and __newindex for numbers, when I try to index a regular number I'm getting:
bad argument #1 to '?' (can't index a number value)

instead of the nicer standard message produced by the luaG_typeerror function:
attempt to index global 'a' (a number value)

Is there a way to use the luaG_typeerror myself? It requires a const TValue * argument and I don't know how to get it in my function.

Ivo


Duncan Cross wrote: [Unfortunately I don't think there is a way to get what you want without a custom modification of Lua, in a way that could potentially make comparisons slightly slower. The only thing I'd mention is that you could use "or" to specify a default - instead of the cumbersome 'if obj.mass and obj.mass > 20' you could do 'if (obj.mass or 0) > 20'. -Duncan On Sun, May 31, 2009 at 7:41 PM, Ivo Beltchev <ivo@roadrunner.com> wrote:
Hi I am embedding Lua in my application and I want to expose some objects to the Lua scripts. Different objects have different sets of properties. For example object1 can have speed and length and object2 can have mass and friction. Currently if the script wants to check if an object has mass>20 it has to do: if (obj.mass and obj.mass>20)... because obj.mass may be nil and then comparing it to 20 fails. So for properties that don't exist I want to create a special object (NIL) that can be safely used in expressions: NIL+x -> NIL NIL..x -> NIL NIL[x] -> NIL #NIL -> 0 NIL==NIL -> true NIL==nil -> true (not so sure about this) NIL>20 -> false ... You get the idea. Then I can simply write: if (obj.mass>20) ... However the __lt metamethod is not being called when I compare NIL to a number. It is used only if the two values are of the same type. Otherwise Lua throws an error. Any ideas how to do this? A NAN value almost d
oes what I want but it can't be indexed, and also NAN~=NAN. Thanks Ivo 

AltStyle によって変換されたページ (->オリジナル) /