by shaneeb · 13y ago · 0 comments
But all in all it's surprisingly effective.
local array = { one, two, threee, four }
A gap is silently created in the array because Lua returns nil for undefined variables. This causes the (#) operator to an return incorrect result and can lead to subtle, difficult-to-find bugs in the program.Lua makes no distinction between nil and a non-existent element, so assigning nil to an array element will also create a gap, rather than shifting the keys of the subsequent elements downward:
local array = { 1, 2, 3, 4 }
array[2] = nil
-- array is now { [1] = 1, [3] = 3, [4] = 4 } local array = {1, 2, 3, 4}
table.remove(array, 2)
-- array is now { [1] = 1, [2] = 3, [3] = 4 }Your first example has a point – perhaps undefined variables should error when accessed – but that's got nothing to do with tables.
Object in Smaltalk.
(Tangentially, I don't agree that conflation of array and object is a good thing)