
Names and Namespaces
The rule for naming a variable in Lua is that it
- consists of letters [a-zA-Z], digits [0-9] and underscores [_]
- may not start with a digit
- may not be a reserved word
and break do end else elseif false for
function goto if in local nil not or repeat return then true while until
There is a convention, rather than a rule, that the name consisting of
a single underscore ( _ ) refers to something whose value is irrelevant,
and that names beginning with an underscore should not be used by the
programmer, but are considered to be in the purlieu of the system.
RiscLua also permits the use of `, !, ?, $ and @, and it may be best to
put these in the same category as _ . RiscLua provides the symbol \ as
an alternative to function, and => as an alternative to
return.
Names are given to values by an assignment statement, which
is characterised by an equals-sign ( = ). On the left is a comma-separated
list of names, and on the right a comma-separated list of expressions
for the values. The two lists do not have to be of the same length. Excess
items on the right are simply ignored, and on the left are assigned the value
nil. The expressions on the right are evaluated in parallel.
A value may be assigned more than one name, or used without any name. A
name may be re-assigned to a different value. It is important to understand
that in other languages, BASIC and C, for example, functions and procedures may
not necessarily be given their names by assignment but by declaration, for
which quite different rules apply. If the word local precedes a list
of names, those names have scope restricted to the current chunk. Otherwise,
the names are taken be keys of the table _ENV, which is the name of the current
environment.
If t is a table and s is a string that satisfies the
rules for a variable-name, for example
s = "heinz77"then the expression t[s] can also be written as t.heinz77. Similarly, in a table expression, inside braces,
[s] = expr;can be written as
heinz77 = expr;
So in any chunk the names of variables appearing in it, its namespace,
belong to one of three classes:
- Locals - declared within the chunk
- Upvalues - declared local in enclosing chunks
- Globals - keys in _ENV