tables
sugar
If keys are omitted in a table constructor expression, they
will be taken to be positive integers, starting at 1 and increasing
by 1. Thus
{ ["+"] = 7, "tom", "dick", ["-"] = false, 4 }
is taken to be
{ ["+"] = 7, [1] = "tom", [2] = "dick", ["-"] = false, [3] = 4 }
If a key
k of a table
t evaluates to
a string which is a legitimate variable-name (say to
"ebenezer") then we can write
t[k]
as
t.ebenezer. Also, in table constructors the
subexpression
ebenezer = .... can be written
in place of
[k] = .... .
If, furthermore, the value of
t.ebenezer is a
function, then an expression
t.ebenezer(t,x,y,...)
can be further abbreviated to
t:ebenezer(x,y,...).
Note that there is no function
t:ebenezer by itself.
The colon simply introduces an abbreviated expression
for a function application.
Those who come to Lua from other languages, particularly those
that stress object-oriented programming, often find themselves
confused over the difference between the dot- and the colon- abbreviations.
The dot-abbreviation helps to
emphasize the fact that updating a table is an assignment of some sort.
The colon-abbreviation provides the illusion that a table has methods,
i.e. functions with an implicit parameter (traditionally called
self ) referring to the table itself. The point of this is
that all the values of the table are in scope (can be referred to)
within the body of a method.