lua-users home
lua-l archive

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


Yes, of course local creates a new local. Your example was with a nested do ... end block. What happens when they are in the same block? This:

local f=1 print(f)
local f print(f)

prints

1
nil

So does this

local f
local f

create two variables f? Before the second I access the first (of course) but after the second local I can only access the second one? Or does the second one just set the value of f to nil?

>From the result of the function defining it seems like the first alternative. If so interesting.

Robert

----- Original Message -----
> From: "Michal Kolodziejczyk" <miko@wp.pl>
> To: lua-l@lists.lua.org
> Sent: Wednesday, 21 November, 2012 1:38:29 AM
> Subject: Re: Forward function declarations - relocal command
> 
> 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
> 
> 
>