lua-users home
lua-l archive

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


On May 19, 2010, at 11:57 AM, Tony Finch wrote:

> 
> nil_t = {}
> setmetatable(t,{ __index = function () return nil_t end })

Yikes! This seems to break Lua's definition of #

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> nil_t = {}
> t = {}
> setmetatable(t,{ __index = function () return nil_t end })
> return #t
0
> return  t[1]
table: 0x100109640
> 

-- Note: t[n+1] is not nil!

> setmetatable(t,{ __index = function (_,x) if x >= 13579 then return nil else return nil_t end end })
> return #t
0
> return t[13579]
nil
> return t[1]
table: 0x100109640
> 

-- Note: t[n+1] is not nil but t[13579] is nil.

So, I guess if you use __index, you must also define __len.

e