-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconst.lua
More file actions
59 lines (51 loc) · 1 KB
/
const.lua
File metadata and controls
59 lines (51 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
do
local constants = {
11,
22,
a = 3,
b = 4,
c = "aaaa",
}
const = setmetatable({}, {
__index = function(t, k)
return constants[k]
end,
__newindex = function(t, k, v)
error("loser")
end,
__call = function()
return {
pairs = function(t)
local function iter(t, k)
local v
k, v = next(constants, k)
if v ~= nil then return k, v end
end
return iter, t, nil
end,
ipairs = function(t)
local function iter(t, i)
i = 1 + i
local v = constants[i]
if v ~= nil then return i, v end
end
return iter, t, 0
end
}
end
})
end
print(const)
print(const.a)
print(const.b)
print(const.c)
local _, err = pcall(function()
const.a = 3
end)
print(err)
for k, v in const().pairs() do
print(("const.%s = %s"):format(k, v))
end
for i, v in const().ipairs() do
print(("const.%s = %s"):format(i, v))
end