Created
December 18, 2012 07:16
-
-
Save prafulliu/4325763 to your computer and use it in GitHub Desktop.
read-only tables in lua.
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
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