lua-users home
lua-l archive

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


> ------------------------------
>Pierre-Yves Gérardy wrote:
> Following the floating point discussion, I tried to look for a 
> compilation of the various gotchas Lua beginners encounter, but I 
> couldn't find one, so I decided to compile one...

This might be more of an "intermediate" gotcha rather than a "newbie"
gotcha, so I'm not sure if you'd want to include it. There are some
"exceptions to the rule" in Lua. Arguably, since "Lua is as Lua does",
"exception" might not be the right word, but they appear to be
exceptions when you stumble across them.

The two I can think of are related to metatables:

1. The __call metamethod only works with a function, not with a table
which itself contains a __call metamethod. Hopefully I've got this
right:

> a=setmetatable({},{__call=function() print "test" end})
> b=setmetatable({},{__call=a})
> a()
test
> b()
stdin:1: attempt to call global 'b' (a table value)
stack traceback:
        stdin:1: in main chunk
        [C]: ?

2. Metamethods in metatables of primitive types are not always invoked.
An example is what I stumbled upon a few days ago: Using
debug.setmetatable() to add a metatable to numbers does not invoke
metamethods __eq, __lt, or __le on numbers. This is documented in the
5.1 reference manual section 2.8 code examples and may be too esoteric
for your list -- feel free to take it or leave it.

Thanks for taking the initiative to put a list together.

John Giors
Independent Programmer
Three Eyes Software
jgiors@ThreeEyesSoftware.com
http://ThreeEyesSoftware.com