[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Comparing userdata with other types
- From: Duncan Cross <duncan.cross@...>
- Date: Sun, 31 May 2009 19:49:09 +0100
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'.
On Sun, May 31, 2009 at 7:41 PM, Ivo Beltchev <firstname.lastname@example.org> wrote:
> 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
> Currently if the script wants to check if an object has mass>20 it has to
> 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 does what I want but it can't
> be indexed, and also NAN~=NAN.