lua-users home
lua-l archive

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


On Sun, May 9, 2010 at 6:23 PM, Klaus Ripke <paul-lua@malete.org> wrote:
> On Sun, May 09, 2010 at 04:49:53PM +0100, Peter Cawley wrote:
>> > Could indexing anything but a table, or at least indexing nil,
>> > return nil instead of throwing an error?
>> > I don't see why x.z conveniently gives nil, if there is no z in x,
>> > but x.y.z requires an extra check for x.y .
>> > This makes working with structures with lots of optional parts pretty
>> > tedious - and if it's external input, basically everything is optional.
>>
>> If you want this, then you can already get it in 5.1 by doing:
>> debug.setmetatable(nil, {__index = {}})
>
> I was considering this,
> yet then I would have empty tables showing up when using
> a nonexistent string value,

How exactly to empty tables show up? You'd get nil when accessing a
non-existent value:
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> debug.setmetatable(nil, {__index = {}})
> =x
nil
> =x.y
nil
> =x.y.z
nil

Note that debug.setmetatable(nil, {__index = {}}) is equivalent to
debug.setmetatable(nil, {__index = function() return nil end}) rather
than debug.setmetatable(nil, {__index = function() return {} end})