lua-users home
lua-l archive

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


2012/11/21 Robert Virding <robert.virding@erlang-solutions.com>:
> 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?

Yes.

> Before the second I access the first (of course) but after the second local I can only access the second one?

Yes. _During_ the second. if you had for example
   local f = f..'a'
or
   local f = function(x) return f..x end
or
   local function f(x) return f..x end
you would still be accessing the first.

> Or does the second one just set the value of f to nil?
The second one does not even check whether there was
a first.

The function debug.getlocal can see all local variables,
even shadowed ones. See my post of 19 Nov in this thread.