lua-users home
lua-l archive

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


Recently I have been writing code that generates proxy tables to (for example) take a slice of an array without deep copying, or to interleave multiple arrays.

In using these I have run into a clear but poorly documented problem with table.unpack() .  Although it respects the __len metamethod, it does not use the __index metamethod and will just return nils if an integer-indexed table element isn't actually part of the array data.  (What a strange design, to allow __len but not __index!--it's as though it's made just general enough to be able to unpack the results of pack!)

Inspection of the lua.org ltablib.c confirms that this isn't just a problem with the embedded Lua we're using: the official code uses lua_rawgeti for array access and doesn't fall back on the metamethod at all.  This is faster than the true equivalent of the Lua-implemented table.unpack() on p. 45 of Programming In Lua, but it should also be possible to check before entering the while loop in (c-implemented) table.unpack for a non-nil __index metamethod and decide whether or not to use the fast existing code or a slower version that uses the metamethod.

Any thoughts on why this isn't so?  And is there a Lua hack (aside from memorization of the results of a proxy table) to make an array-like object with an __index metamethod but no array data work with table.unpack()

-Ben Kalafut