lua-users home
lua-l archive

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


On Fri, Nov 25, 2011 at 10:02 PM, HyperHacker <hyperhacker@gmail.com> wrote:
> On Fri, Nov 25, 2011 at 19:17, Benoit Charland <benoitc@hotmail.com> wrote:
>> The problem is that all functions from the table library honor the
>> __len metamethod, but not __index or __newindex.

Although 5.2 now allows '#' operator overloading on public interfaces
of table-based objects via __len, I'm not seeing why this change [1]
also affected the table library, whose low-level functions have not
been applicable to those public interfaces.  What is the use case for
the table library change?  The only reasons I can think of are (low
level) algorithmic ones of questionable utility:

  -- Sort a sparse array.  Is this common? and why just add `i,j`
arguments to `sort`?
  local t = setmetatable({[1]=4, [50]=2, [100]=3}, {__len = function()
return 100 end})
  table.sort(t, function(a,b) return (a or 0) < (b or 0) end)
  for i=1,#t do print(t[i]) end

[1] http://lua-users.org/lists/lua-l/2010-12/msg00653.html