lua-users home
lua-l archive

Re: Lookup nil table index

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


On 30-Nov-06, at 10:54 AM, Javier Guerra wrote:
anything [nil] => nil
currently both forms generate an error, and there's very little hope that it
may change.
Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> return ({})[nil]
nil
-- *Setting* generates the error:
> ({})[nil] = 42
stdin:1: table index is nil
stack traceback:
 stdin:1: in main chunk
 [C]: ?
-- But you can make a table which maps nil onto something other than nil: > a = setmetatable({}, {__index=function(_, k) if k == nil then return 42 end end})
> =a[42]
nil
> =a[nil]
42

AltStyle によって変換されたページ (->オリジナル) /