Wikipedia:Guide to Scribbling/Programmers' Quick start Guide to Lua

Here are some short points about Lua, for those who already know other computer programming languages and how to program. They focus mainly upon what you might find different in Lua.

  • Lua is dynamically typed. There's no static typing at all.
  • From a syntactic point of view, think BASIC (or even COMAL) without line numbers and colons rather than C/C++/Java, Lisp/Scheme, or Forth.
    • There's no begin, but most control structures have an end
    • for needs a do and if needs a then.
    • { ... } denote a table (expression), not a block of code.
    • Indentation, extra whitespace, and (with one exception) newlines don't change syntax or semantics.
  • Almost everything is a table. If it isn't a table, it's a string, a number, a boolean, a function, or a nil.
    • Libraries are tables. string.gmatch is the "gmatch" entry in the table named by the global variable string.
    • Arguments that you receive from MediaWiki are tables. But they're a bit special.
    • Arrays are tables that follow a specific convention. The numerical fields in the array start at one, and run contiguously with no "holes" with nil values in the middle of the array.
  • p.q is syntactic sugar for p["q"].
  • function p.q is syntactic sugar for p["q"] = function.
  • function builds a function. It doesn't declare it. Functions are first-class objects and can be assigned to variables, placed in tables, serialized into strings, and deserialized back out again. Think interpreted, not compiled.
  • Functions and tables start off anonymous, as function () ... end and { ... }, and gain names when assigned to variables.
    • You can create anonymous functions and tables on the fly in the middle of an expression.
    • You can return (anonymous) functions and tables using return.
  • Local variables are cheap. Tables are expensive. a.b.c may look like two simple member references as in other languages. It isn't.
  • This isn't C. if (0) takes the then branch. Only false and nil are false. Even "" is true.
  • Tables can have both numeric and string entries. The two don't overlap. t[1] is distinct from t["1"].
  • Uninitialized variables and nonexistent fields in tables are nil.
  • or and and both have shortcut evaluation.
  • = is assignment, == is equality comparison.
  • not is boolean negation, but ~= is inequality comparison.
  • [[...]] creates a string literal. To avoid visual confusion, use "..." or '...' for strings containing wikitext.
    • Combined with --, [[...]] creates a multiline comment. Think of it as #if 0 ... #endif.
    • Putting = between the brackets — e.g. [===[...]===] — makes these so-called long brackets longer.
  • Functions and tables are passed by reference, not by value.
  • String operations trade space for time. Don't repeatedly concatenate onto the end of a string. Build up its individual parts in a table and use table.concat().
  • * by the reciprocal is faster than /.
  • return and break can only occur at the end of a block.
    • There's no continue.
    • There's no goto.
    • next is a global variable, not a keyword. By default, it references a function — first class objects, remember. — that does iteration over a table.
  • # expects the array convention. If your table isn't adhering to that convention, you'll get funny results.
  • Use nil == next(table) to check for a table being empty.
  • When tonumber() fails, it returns nil.
  • This isn't C. We have an exponentiation operator, and it is ^.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.