Lua notes

From Helpful
Jump to navigation Jump to search
📃 These are primarily notes, intended to be a collection of useful fragments, that will probably never be complete in any sense.


Variables

scope and assignment

Arrays / tables

types and conversions

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.
nil
boolean
number  always (float)
string
function
userdata
thread
table


You'll also find frequent use of

Casting and coercion

  • tostring
note that .. coerces things to strings
if something has a __tostring" field, it will be called(verify)
you may sometimes want string.format for more control, which is a printf-alike

If the metatable of e has a


  • tonumber(e [, base])


  • string.byte(s, i)[1]
return characters as number
  • string.byte(s, i,j)[2]
return characters as list of numbers

Conventions

CamelCase for classes, factories

underscores for objects, instances, functions,

Operators

Aside from + - * / and %, there is also ^ (pow)


Boolean

and
or 
not 
==   equal to
~=   not equal to
<
>
<=
>=

Also

#    length of table or string
..    string concat


Bitwise operators

See also:

String manipilation

http://www.lua.org/manual/5.1/manual.html#5.4


modules

Specific implementation notes

Errors

function or expression too complex

Often amounts to 'too many arguments' (the VM has only so many registers)

In that case, the fix is often "pass an array" and/or "build that array in chunks".

attempt to concatenate a userdata value

attempt to concatenate global 'varname' (a nil value)

attempt to index a nil value