lua-users home
lua-l archive

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




On 07/08/16 05:55 AM, Dirk Laurie wrote:
2016-08-07 6:58 GMT+02:00 Tim Hill <drtimhill@gmail.com>:

table.len = function(t) return #t end
But

table.frontier = function(t)
    local mt = getmetatable(t)
    local len = #setmetatable(t)
    setmetatable(t,mt)
    return len
end

is less trivial, and sufficiently cumbersome to jusitfy implementation
at a lower level.

How about

table.len = function(t)
assert(type(t) == "table", "bad argument #1 to 'len' (table expected, got "..type(t)..")")
  return #t
end

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.