lua-users home
lua-l archive

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




On 2020-02-29 1:31 p.m., Egor Skriptunoff wrote:
On Sat, Feb 29, 2020 at 5:42 PM Italo Maia wrote:

    the error when trying to index a null value is a good thing.



Let's assume the statement
v = t[a][b][c][d][e]
Each of 5 indexing steps might result in nil.
Sometimes you want to raise "Attempt to index a nil value" exception,
sometimes you want to ignore the exception.

If nil is indexable, then it's pretty easy to raise the exception
v = assert(t[a][b][c][d])[e]

But if nil is non-indexable, it's not easy to ignore the exception
v = (((((t or {})[a] or {})[b] or {})[c] or {})[d] or {})[e]

Indexable nil looks like a better alternative.

Would indexable nil reduce runtime error detection?   No.
Almost always another exception will be raised soon:
"Attempt to perform arithmetic on a nil value" and the like.


v = (pcall(function() t[a][b][c][d][e] end) or nil) and t[a][b][c][d][e]

slightly cleaner.