Skip to content

Instantly share code, notes, and snippets.

@prafulliu
Created December 18, 2012 07:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prafulliu/4325763 to your computer and use it in GitHub Desktop.
Save prafulliu/4325763 to your computer and use it in GitHub Desktop.
read-only tables in lua.
function readOnly( t )
local proxy = {}
local mt = {
__index = t,
__newindex = function ( t, k, v )
error("attempt to update a read-only table", 2)
end
}
setmetatable(proxy, mt)
return proxy
end
days = readOnly{"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}
print(days[1])
days[2] = "Noday"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment