lua-users home
lua-l archive

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



On 08/09/2006, at 10:10 AM, Sam Roberts wrote:


Surprise depends on your expectations, and that depends more on you
than lua!

Indeed. My surprise is going away now. ;)


The example shows multiple local variables being created with the same
name. Example does this with a for loop, and your example unrolls the
loop into consecutive local statements, but its the same thing
happening.

Those examples do not show two local variables at the same scope level.

      local x = x         -- new 'x', with value 10
       print(x)            --> 10
       x = x+1
       do                  -- another block
         local x = x+1     -- another 'x'
         print(x)          --> 12
       end
       print(x)            --> 11

The second "local x" behaves as I expected, to define a new variable, as it is inside a different scope (do ... end).

Example does this with a for loop, and your example unrolls the
loop into consecutive local statements, but its the same thing
happening.


Not really, because of the different scoping levels. That final x was 11, not 12. If you take out the "do ... end" it takes the final value of x which will be 12.

Thanks to everyone for all the constructive comments. :)

- Nick