lua-users home
lua-l archive

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


On Sun, May 9, 2010 at 12:23, Richard Hundt <richardhundt@gmail.com> wrote:
> Klaus Ripke wrote:
>>
>> Could indexing anything but a table, or at least indexing nil,
>> return nil instead of throwing an error?
>
> This doesn't require any changes to the language:
>
> debug.setmetatable(nil, { __index = { } })
>
> Cheers,
> Rich
>

I'm tempted to agree with the idea of nil returning nil when indexed,
but maybe issuing a warning. On one hand it seems like a way for bugs
to go unnoticed, while on the other it's nice to be able to load a
table a few levels deep from a config file and index it without
worrying about missing entries.
What I've been doing is recursively applying __index methods to each
element of a table that return an empty table with that same __index
method, so that a.b.c.d returns {} if b, c or d are nil.

-- 
Sent from my toaster.