-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.lua
More file actions
58 lines (52 loc) · 1.23 KB
/
Copy pathutility.lua
File metadata and controls
58 lines (52 loc) · 1.23 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
local m = {}
local immutable_mt = {
__newindex = function(t,k,v)
error("Write to table is not allowed")
end,
__index = function(t,k)
if(rawget(t,k) == nil) then
error(string.format("Access to nonexisting key: %s", k))
else
return rawget(t,k)
end
end
}
function m.immutable_table(t)
return setmetatable(t, immutable_mt)
end
function m.typecheck(...)
local mt = {
__concat =
function(a,f)
return function(...)
--do paramter type check, must match one by one.
assert(#a == select("#",...),
string.format("Incorrect number of paramters, expect %d, got %d.", #a, select("#", ...)))
for i=1,#a,1 do
assert(a[i] == type(select(i, ...)),
string.format("Parater #%d does not match, expect %s, got %s.", i, a[i], type(select(i, ...))))
end
return f(...)
end
end
}
return setmetatable({...}, mt)
end
local docstrings = setmetatable({}, {__mode = "kv"})
function m.get_docstring(f)
return docstrings[f];
end
function m.docstring(...)
local mt = {
__concat =
function(a,f)
--store docstring, only accept the first parameter to the docstring function.
docstrings[a[1]] = a[2];
return function(...)
return f(...)
end
end
}
return setmetatable({...}, mt)
end
return m