lua-users home
lua-l archive

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




On Thursday, August 21, 2014, Thiago L. <fakedme@gmail.com> wrote:

On 21/08/14 11:14 PM, Andrew Starks wrote:



On Thu, Aug 21, 2014 at 8:30 PM, Ahmed Charles <acharles@outlook.com> wrote:
I'm curious as to why the index metamethod is a fallback when the value does not exist while the len metamethod is not a fallback to the normal length not making sense?

I suppose that would prevent using len on tables at all. Was that the rational?

On Aug 5, 2014, at 3:53 AM, "Luiz Henrique de Figueiredo" <lhf@tecgraf.puc-rio.br> wrote:

>> 3) I wish __call were respected if you give the function type a
>> metatable, and basic C operations were exposed to Lua, like
>> getmetatable(0).__add = <C function to add 2 numbers>
>
> A central point in Lua semantics is that metamethods correspond to
> events that Lua cannot handle. (Metamethods were originally called
> fallbacks, for this reason.) Addition of two numbers is not such an
> event and it is never performed via a metamethod, even if you define one:
>
>    Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
>    > debug.setmetatable(0,{__add=print})
>    > a=1
>    > b=2
>    > =a+b
>    3
>    ... but this works:
>    > debug.setmetatable(0,{__call=print})
>    > a(10,20,30)
>    1    10    20    30
>


When does #t ever equal nil, outside of a __len metamethod?

-Andrew
Well Lua could do a (probably O(n)) check to see if the table is a sequence, and if it isn't then call __len...

My point is that even an empty table has a length: 0. It's an empty sequence. So the op's suspicion was correct: __len would never be called if it was a fallback.