lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Alexander Gladysh <> dixit:

> BTW, does any one know more strict/formal definition for the way Lua
> values and variables are bound together (including assignment and
> function argument / return value passing)?
> Related questions are rather popular topic for people coming to Lua
> from C++ side. I'd like to be able to aswer it better. :-)
> Alexander.

All variables hold references, never data directly. As a consequence, after
   t = {1, "1", {1}, function() print 1 end}
t actualy holds 4 references (pointers, under the hood), addressable as t[1]..t[4], to 4 pieces of data.
However it's implemented concretely, after
   x = whatever
a symbol table will bind 'x' to a pointer to whatever. 'x' will not be bound to whatever directly.

But this is meaningless (I mean it has no consequences) for so-called "immutable" things (numbers, strings) for they cannot be changed on place -- so they still behave like plain values, like if the name were (was?) bound to the actual data, directly.

You may search for literature about common dynamic languages (esp. python) for they are implemented the same way. (The ordinary distinction between passing by reference/value is rather misleading, rather the actual value *is* a reference. The binding scheme is "pointer reference". It does not make a full indirection because the reference is the address itself -- compare with symbolink links.)


la vita e estrany