|
I am not very familiar with Perl, but AFAIK, Perl's auto-vivification automatically creates array elements even when just *reading* from the array. This is not what I suggest Lua should do. Lua should only create tables when *assigning*. When reading, just return nil and don't create anything.Italo Maio wrote:Perl has auto-vivification and it creates way more problems than it solves. Also agree this should not be part of the language and the error when trying to index a null value is a good thing.
This will work, and there are indeed multiple ways to code v = t[a][b][c][d][e] to avoid throwing an error. To the best of my knowledge, the code example that posted (with lots of "and"s), executes the fastest (and is also relatively easily readable).Soni "They/Them" L. wrote:> v = (pcall(function() t[a][b][c][d][e] end) or nil) and t[a][b][c][d][e]
Coda Highland wrote:This comes up very, very frequently.This is absolutely a non-starter for inclusion in stock Lua, but for your own programs there are a number of solutions that have been proposed and/or implemented.
Could you elaborate why this is a non-starter?I have seen a number of solutions (including solutions that use metatables to define dynamic arrays, which are useful only when assigning) and also offered my own preferred code examples, which manage do the job in my own programs, but none of these workarounds are as simple -- and consistent -- as writing v = t[a][b][c][d][e].
-- Anton