假设我有一个序列: a = { 10, 12, 13 } 该序列的长度(#a)为3. 现在,假设我执行以下操作: table.insert(a, nil) (或[#a 1] =零.) 这会以任何方式影响桌面吗? 这个问题的答案是决定性的,还是这种“
a = { 10, 12, 13 }
该序列的长度(#a)为3.
现在,假设我执行以下操作:
table.insert(a, nil)
(或[#a 1] =零.)
这会以任何方式影响桌面吗?
这个问题的答案是决定性的,还是这种“未定义的行为”?
在Luas上我检查过(Lua 5.1,Lua 5.3)这不会影响表格.但我想知道这是否是我不能依赖的“未定义行为”.
该手册仅讨论在序列的中间添加nil,但它没有(根据我的解释)谈论将它添加到序列的末尾.
在序列中添加nil值根本不起作用.实际上,一个表不能保持零值.从 the manual开始:Tables can be heterogeneous; that is, they can contain values of all types (except
nil). Any key with valuenilis not considered part of the table. Conversely, any key that is not part of a table has an associated valuenil.
因此,表{10,12,13,nil}相当于{10,12,13},两者都是序列.
类似地,一个非序列的例子:一个表{10,20,nil,40}相当于{[1] = 10,[2] = 20,[3] = nil,[4] = 40}或{[ 1] = 10,[2] = 20,[4] = 40}
