Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created October 19, 2010 10:11
Show Gist options
  • Save garybernhardt/633959 to your computer and use it in GitHub Desktop.
Save garybernhardt/633959 to your computer and use it in GitHub Desktop.
x = function()
y = function()
local table = 5
end
local old_table = table
y()
assert(table == old_table and table ~= 5)
end
x()
@benjaminws
Copy link

I didn't notice the behavior until I did an operation on the local table with the built in table library.. Seems the table library 'namespace' is what gets stomped on.

Example:

> x = function()
>>     y = function()
>>         local table = 5
>>     end
>>     local old_table = table
>>     y()
>>     table.insert(old_table, 4)
>>     assert(table == old_table and table ~= 5)
>> end
> x()
stdin:7: attempt to call field 'insert' (a nil value)
stack traceback:
        stdin:7: in function 'x'
        stdin:1: in main chunk
        [C]: ?

Table library: http://lua-users.org/wiki/TableLibraryTutorial
Gist for you.. http://gist.github.com/634253

@nbarendt
Copy link

Hmm, works for me

nickmacbookpro:lua nbarendt$ ./bin/lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> x=function()
>>   y= function()
>>     local table=5
>>   end
>>   local old_table = table
>>   y()
>>   table.insert(old_table, 4)
>>   assert(table == old_table and table ~=5)
>> end
> x()
> 

@benjaminws
Copy link

Odd.. Works for me too, on another machine.. Starting to think I have a bum build on my fedora machine..

bsmith@ubuntu!1029 J:1 M:command
-> lua  
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> x = function()
>>     y = function()
>>         local table = 5
>>     end
>>     local old_table = table
>>     y()
>>     table.insert(old_table, 4)
>>     assert(table == old_table and table ~= 5)
>> end
> x()

Also, Nick, try 4 spaces before your snippet to get it to

preserve

@nbarendt
Copy link

Ben, thanks, forgot my Markdown :-(
(or to preview before submitting, :-((

@benjaminws
Copy link

S'cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment