විකිපීඩියා:Guide to Scribbling/Programmers' Quick start Guide to Lua
Appearance
ප්රධාන පිටුව: Wikipedia:Guide to Scribbling
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 anend
for
needs ado
andif
needs athen
.{ ... }
denote a table (expression), not a block of code.- Indentation, extra whitespace, and (with one exception) newlines don't change syntax or semantics.
- There's no
- 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 variablestring
. - 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.
- Libraries are tables.
p.q
is syntactic sugar forp["q"]
.function p.q
is syntactic sugar forp["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 thethen
branch. Onlyfalse
andnil
are false. Even""
is true. - Tables can have both numeric and string entries. The two don't overlap.
t[1]
is distinct fromt["1"]
. - Uninitialized variables and nonexistent fields in tables are
nil
. or
andand
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.
- Combined with
- 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
andbreak
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.
- There's no
#
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 returnsnil
. - This isn't C. We have an exponentiation operator, and it is
^
.