Jump to content

Lua Programming/mistake

From Wikibooks, open books for an open world

Biggest sins of Lua

[edit | edit source]
  • object:method() -- Did you notice the second dot, aka. colon? It's just as easy to forget, as You have to tell explicitly this is a method, not just a function reference stored in a field.
  • local array={1,2,3} ; for i,value in ipairs(array) do .. end -- Iterators save creating a real iterator object, passing 2/3 values instead, like a tuple (no tuples, so multiple return values).
  • global by default: /run local count=0  ; for k,v in pairs(_G) do count=count+1 end  ; print("Variables in global namespace:", count) -- Run in the most widespread game with embedded lua: prints 48680 ... that is 48 thousand.
  • ` ~= ` -- Once you figure out what this is, you will know why it's here.

Common Mistakes in Lua Programming

[edit | edit source]
  • object.method() -- Used to js/java/python/ruby/scala/ceylon/kotlin/youNameIt?
  • local array={1,2,3} ; for i,value in array do .. end -- Error: attempt to call a table value. Note: array is the table. It really does call it, thus a __call() metamethod can return the iterator function, just to fool and confuse lua beginners even more.
  • Forgetting to separate function arguments with comma symbols
  • Missing a closing bracket from an argument list or expression
  • Using spaces in identifier names for variables and functions
  • Don't mix up 'nil' with 'false' - ie. 'nil' does NOT equal 'false'.