Skip to content

Instantly share code, notes, and snippets.

@ykst
Created November 9, 2012 01:45
Show Gist options
  • Save ykst/4043199 to your computer and use it in GitHub Desktop.
Save ykst/4043199 to your computer and use it in GitHub Desktop.
Lua table hack
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