lua-users home
lua-l archive

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


> 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?

Yes. BTW, you do not need the debug libray to make your point:

  do
    local state = 0
    f = function (x) if x then state = x else return state end end
  end
  
  f1 = f        -- both f and f1 refer to the same function now
  f(10)
  print(f1())   --> 10

(That is why sometimes we prefer to call them 'closures'; I think this
name makes clearer that it is a muttable object.)

-- Roberto