Posted 31 January 2016 - 07:54 PM
EDIT: This is solved. Working function:
This is what I have so far.
However when running packagePrivateTable on a table, it returns a table with nothing in it (attempting to access values returns nil). What am I doing wrong here?
local function readonlytable(table)
return setmetatable({}, {
__index = table,
__newindex = function(table, key, value)
error("Attempt to modify read-only table")
end,
__metatable = false
});
end
function packagePrivateTable(tbl)
for key, value in pairs(tbl) do
if type(value) == 'table' then
tbl[key] = packagePrivateTable(tbl[key])
end
end
return readonlytable(tbl)
end
Original Post
I am trying to make a recursive readonlytable() that will make tables inside of the original tables into read-only tables.This is what I have so far.
local function readonlytable(table)
return setmetatable({}, {
__index = table,
__newindex = function(table, key, value)
error("Attempt to modify read-only table")
end,
__metatable = false
});
end
function packagePrivateTable(tbl)
local workingTable = {}
local argTable = tbl
local workingCount = 0
for key, value in pairs(argTable) do
if type(key) == 'table' then
workingTable[key] = packagePrivateTable(argTable[key])
end
end
return readonlytable(workingTable)
end
However when running packagePrivateTable on a table, it returns a table with nothing in it (attempting to access values returns nil). What am I doing wrong here?
Edited on 31 January 2016 - 07:24 PM