I wrote a Lua programming handout for my job for use by the long-time C programmers in our department. In it there is a section called "Footguns" where I listed these 5 gotchas.
- The "key" in t.key is the constant string "key", not a variable named "key".
- A table that contains at least one nil is a hash, not an array. This means that #t may not return the number of elements in the table.
- Be careful to distinguish between a period and a colon when calling functions. t.f() is not the same thing as t:f().
- Lua arrays start at 1, not 0.
- Only false and nil are false. 0 is true.
- Undeclared variables are global.
On Dec 1, 2010, at Dec 1 3:28 PM, 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...
Right now, I can think of two of them:
- Floating point inaccuracies. A simplified version of Patrick Rapin's "math.compare (x,y,delta)" [1] could be proposed as a workaround. - The fact that you have to declare locals before using them.
Are there other potential traps? On what did you stumble while learning the language?
Kind regards, -- Pierre-Yves
|