From f3c354d396a38033dac28ece6f6220e75cef5aec Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Tue, 1 Jul 2014 20:51:07 +0200 Subject: [Lua] Día 2 (sin terminar) --- lua/strict.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lua/strict.lua (limited to 'lua/strict.lua') diff --git a/lua/strict.lua b/lua/strict.lua new file mode 100644 index 0000000..1c70442 --- /dev/null +++ b/lua/strict.lua @@ -0,0 +1,25 @@ +local _private = {} + +function strict_read(table, key) + if _private[key] then + return _private[key] + else + error("Invalid key: " .. key) + end +end + +function strict_write(table, key, value) + if _private[key] then + error("Duplicate key: " .. key) + else + _private[key] = value + end +end + +local mt = { + __index = strict_read, + __newindex = strict_write +} + +treasure = {} +setmetatable(treasure, mt) -- cgit v1.2.3