Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local map = {}
- local function mod_pairs(_t)
- local t = {}
- for k, v in pairs(_t) do
- t[#t + 1] = k
- end
- local i = 0
- return function()
- i = i + 1
- return t[i], _t[t[i]]
- end
- end
- local copy = function(_t)
- local t = {}
- for k, v in pairs(_t) do
- t[k] = type(v) == "table" and copy(v) or v
- end
- return t
- end
- local sm = function(_t, _mt)
- map[_t] = setmetatable(copy(_t), _mt)
- for k, v in mod_pairs(_t) do
- _t[k] = nil
- end
- for i = 1, _mt.__len or #_t do
- _t[i] = {}
- end
- return setmetatable(_t, {
- __index = map[_t];
- __newindex = function(self, key, value)
- map[_t][key] = value
- end;
- })
- end
- -- Testing
- local lentest = sm({1, 3, 4, 3, 43, 34, 3}, {__len = 0}) -- setting the __len to 0
- print(#lentest) --> 0
- lentest[5] = 7
- print(lentest[5]) --> 7
- print(#lentest) --> 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement