12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- local __meta = {
- balances = {},
- payments = {},
- alls = {}
- }
- local david;
- local mt = {
- __index = function(table, key)
- return "dynamic value for " .. key
- end,
- __newindex = function(table, key, value)
- error(key .. " is a read only property")
- end
- }
- setmetatable(__meta.balances, mt)
- setmetatable(__meta.payments, mt)
- local globalMeta = {
- __index = function(table, key)
- print("Accessing undefined key:", key)
- if key == "balances" then
- return __meta.balances
- elseif key == "payments" then
- return __meta.payments
- end
- return __meta.alls[key]
- end,
- __newindex = function(table, key, value)
- print("set " .. key .. " dynamically to " .. value)
- __meta.alls[key] = value
- end
- }
- setmetatable(_G, globalMeta)
- -- Accessing existing keys
- print(balances.key1) -- Output: value1
- print(balances.key2) -- Output: value2
- -- balances.key1 = "value 1"
- -- Accessing undefined key
- print(balances.key3)
- print("xxx")
- print(payments.key3)
- print("v => " .. cesar)
- print("v => " .. david)
|