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