lua-users home
lua-l archive

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


On 21.11.2012 01:15, Robert Virding wrote:
> Sorry I don't WHY there is a problem. I assume I would get the same effect with a recursive function. So
> 
> local f
> local f = function () <ref to f> end
> 
> doesn't work but the following doesn't
> 
> local f
> f = function () <ref to f> end
> 
> The second one is from the 5.2 manual as the expansion for
> 
> local function f () <ref to f> end
> 
> Why is it like that? Does it need to be like that? Have I missed something in the manual? Or is it just an implementation detail?

Because "local" creates a new local. Try this to see the difference:

local f=1
do
  print(f)
  local f=2  -- creating another local variable
  print(f)
end
print(f)

local g=1
do
  print(g)
  g=2        -- (re)using a local variable
  print(g)
end
print(g)

Regards,
miko