lua-users home
lua-l archive

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


2016-01-27 19:09 GMT+02:00 Stephan Hennig <sh-list@posteo.net>:

> A test shows that none of these functions, that
>    I'd consider part of "the table library", respect a table's __index
>    metamethod.
>
> -- A table with three entries.
> local t = { 1, 2, 3 }
> -- We add a fourth one that is accessible via
> -- the __index metamethod.
> local mt = {
>   __index = { [4] = 4 },
> }
> setmetatable(t, mt)

They do respect the __index metamethod, but the primitive
length calculation does not. Since you have failed to provide
__len, #t is still 3. The documentation

> mt.__index = function() return 4 end
> table.concat(t)
1234

The manual does state clearly that "the table must be a proper sequence
or have a __len metamethod ". That's enough.