Created
November 9, 2012 01:45
-
-
Save ykst/4043199 to your computer and use it in GitHub Desktop.
Lua table hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function protect_table(tbl) | |
return setmetatable ({}, { | |
__index = tbl, | |
__newindex = function (t, n, v) | |
error ("attempting to change constant " .. | |
tostring (n) .. " to " .. tostring (v), 2) | |
end | |
}) | |
end | |
local function copy_on_write_table(tbl) | |
return setmetatable ({}, { | |
__index = tbl, | |
__newindex = function(t, n, v) | |
setmetatable(t, {}) | |
for i, v in pairs(tbl) do | |
t[i] = v | |
end | |
t[n] = v | |
end | |
}) | |
end | |
table.copy_on_write = copy_on_write_table | |
table.protect = protect_table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment