lua-users home
lua-l archive

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


Nevin Flanagan wrote:
>
> On Jan 9, 2010, at 3:26 PM, Tom N Harris wrote:
>> __index can be a table instead of a function. Can __len be a number?
> 
> It seems to me that it could be, if it can't be now. That seems to fit well into the Lua idiom.

The manual is misleading about __len. The pseudo-code says...

    function len_event (op)
      --[[ snip ]]--
        local h = metatable(op).__len
        if h then
          return (h(op))     -- call handler with the operand
        elseif type(op) == "table" then
          return #op                -- primitive table length
      --[[ snip ]]--

If that were the case, then this would be possible...

    T=setmetatable({},{
     __len=setmetatable({gth=0},{
      __len=function(o) return o.gth end
     })
    })
    getmetatable(T).__len.gth = 3
    print(#T)

An unrelated minor bug in 5.2 (actually 5.1 has it too):

    S=[[]]
    T={}
    C=coroutine.create(function() end)
    print(type(S),type(T),type(C))
    print(S<T)
    print(T<T)
    print(S<C) -- error message is wrong


-- 
- tom
telliamed@whoopdedo.org