lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> so if I understand correctly I can make a _real_ read-only table
> only with the readonlytable() function?
No, you could do the same thing with the inline code if it was more
like the code in your function:

x = setmetatable({}, {
    __index    = x,
    __newindex = function(x, key, value)
                    error("Attempt to add a new or modify an exists
item to a read-only table")
                end,
    __metatable = false
});

> And in this case what happens with the "original" table?

It lives on because your table holds a reference to the metatable and
the metatable holds a reference to the old table. But hopefully no
other code can get a reference to it, because then it wouldn't really
be read-only!