lua-users home
lua-l archive

Re: Depth indexing of nil

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


> I want to use the value of ‘e’, if it exists. If it doesn’t, that’s fine, I do something else. This:
> 
> if a.b.c.d.e == something then
> something-else
> end
> 
> only works if everything down to e exists. If anything else doesn’t, then Lua spits the dummy and tells me I’m indexing a nil value. This is technically correct, but actually, I don’t care. So what I end up having to do is:
> 
> if a and a.b and a.b.c and a.b.c.d and a.b.c.d.e and a.b.c.d.e == something then
> something-else
> end
My prefered technique is this one:
 -- put this somewhere in your code; it can be global or local:
 E = {}
 if ((((a or E).b or E).c or E).d or E).e == something then ...
-- Roberto

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