lua-users home
lua-l archive

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


__index and __newindex only apply when rawget(table, key) == nil

On Mon, Jun 29, 2009 at 7:35 PM, Sean Riley<sean@metaplace.com> wrote:
> This code doesn’t invoke the “__index” metamethod.
>
> tt = {'a', 'b', 'cc'}
>
> mt = {
>
>   __newindex = function(table, key, value)
>
>                     error("Attempt to modify read-only table")
>
>                   end,
>
>  __index = function(table, key, value)
>
>                     error("Attempt to access read-only table")
>
>                   end,
>
> }
>
> setmetatable(tt, mt)
>
> tt[2] = 1
>
> I don’t understand exactly why..
>
> -------------------
>
> Sean Riley
>
> "All problems in computer science can be solved by another level of
> indirection", Butler Lampson, 1972