lua-users home
lua-l archive

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


On Mon, Jun 27, 2011 at 16:25, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> but that isn't a reason to not add them to the language itself.
>
> A language does not grow in a healthy way by adding things for which you
> cannot find a reason to omit.
>
>> It can also be done by creating a custom
>> allocator but that is messy and costs performance.
>
> There is *no* internal allocator. The one set by luaL_newstate is
> a custom allocator. Any performance is gained or lost there.
>
>> change the current behavior so that t[x][y][z] will just return nil
>> if t[x][y] is nil and t[x][y][z] = 'abc' would set t[x][y] = {} if
>> t[x][y] was nil
>
> This is easy:
>
> debug.setmetatable(nil,{__index=function()end})
>
>

That only satisfies the first half of the request:
> debug.setmetatable(nil,{__index=function()end})
> t={}
> =t.x.y.z
nil
> t.x.y.z = 4
stdin:1: attempt to index field 'y' (a nil value)

and since it's not standard, it means potentially breaking or
conflicting with modules that expect something else or do a similar
trick themselves.

You could hack up some magic with __index and __newindex to make the
latter work, but you'd have to explicitly apply it to each table.

-- 
Sent from my toaster.