3 ms·
The ideal fix would to allow tables to store nil values, instead of always treating nil as a "hole". Instead of assigning nil to remove a value, you would call
by ufo 6y ago
The ideal fix would to allow tables to store nil values, instead of always treating nil as a "hole". Instead of assigning nil to remove a value, you would call a separate "delete" function or operator. This way there would still be only one table type instead of separate types for tables and arrays.
Some early alpha versions of Lua 5.4 played around with this idea but it turns out that it would break too many things. I'd expect that if Lua ever makes a major jump to Lua 6.0 then nils in tables will likely be one of the main changes.
- anonymoushn 6y agoI'd like to read more about this. It would be a little unfortunate to end up with Python's "x in y" and "del y[x]" just to differentiate between missing entries and present entries set to nil. On the other hand, my not-very-well-considered preferred approach creates some asymmetry between arrays and maps as to what values can be stored. Or it perpetuates the existing asymmetry between varargs and tables...
- pansa2 6y agoFrom http://lua-users.org/lists/lua-l/2018-03/msg00155.html http://lua-users.org/lists/lua-l/2018-03/msg00155.html. Looks like this was in the first pre-alpha release of 5.4 but removed shortly afterwards: > t[i]=undef is a special syntax form that means "remove the key 'i' from table 't'". You can only use 'undef' in three specific forms: t[i] = undef -- remove a key from a table t[i] == undef -- test whether a table has a key t[i] ~= undef -- test whether a table has a key
- anonymoushn 6y agoThanks! I like this proposal a lot.