lua-users home
lua-l archive

Re: Possible bug with the length operator

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


I have a current usecase in public code which uses a grey area (as far
as I can see) in the current len operator definition.
My code creates holes to truncate items from a list but the len
operator is never called while the table has holes in it.
This is a cut down example of the method:
#!/usr/bin/env lua
-- Items are created or cleared to meet the required new_size
function update(t, new_size)
 local tlen = #t -- cache item size
 for i=1, math.max(tlen, new_size) do
 if i > new_size then
 t[i] = nil -- collect item
 else
 if not t[i] then
 t[i] = true -- create item
 end
 -- do something
 end
 end
end
local t = {}
for _, new_size in ipairs{10, 1, 100, 60, 30, 65, 8, 64} do
 update(t, new_size)
 assert(#t == new_size, "size mismatch")
 print(#t, new_size)
end
I've never seen an incorrect size returned by #t so far but is this
behaviour subject to change in later lua versions?
Mason

AltStyle によって変換されたページ (->オリジナル) /