lua-users home
lua-l archive

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


On Sun, Jan 10, 2010 at 10:01 PM, Tom N Harris <telliamed@whoopdedo.org> wrote:
> 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)

I don't see any error in the implementation here. Try this:

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

?

> 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

I get this bad error message too.

-- 
-Patrick Donnelly

"Let all men know thee, but no man know thee thoroughly: Men freely
ford that see the shallows."

- Benjamin Franklin