lua-users home
lua-l archive

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


On 2/4/11, mauro iazzi <mauro.iazzi@gmail.com> wrote:
> As I see it a reference can be a first class value, for example tables
> are first class values and are references. If a type is immutable like
> Lua strings or numbers there is not effective difference, in fact
> strings are managed as hashes of a single string copy.
> For functions you can do
>
> do
>   local v = 1
>   a = function () print(v) end
> end
> b = a
> debug.setupvalue(b, 1, 2)
> a()
>
> To me this means that b is a reference to the same function as a. Am I
> correct?
>
> mauro
>
>

Both b and a hold references to the same function.

debug.setupvalue(b, 1, 2) changes the underlying function; b =
function(...) return ... end does not.

You can do

a = setmetatable({},{__call = function(t,...) return ... end})

and have a reference to a "function" that can be changed in the
background (and that can keep "private" state in t).