lua-users home
lua-l archive

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


Op Do., 23 Aug. 2018 om 10:20 het Roland <roland.yonaba@gmail.com> geskryf:
>
> Hi List,
>
> I was experimenting some things with various versions of Lua. Especially
> this :
>
> print(pcall(pairs, true))
>
> Under Lua 5.1.5, it returns : false     bad argument #1 to '?' (table expected,
> got boolean)
> With Lua 5.2.2, it returns : false      bad argument #1 to 'pairs' (table
> expected, got boolean)
> Under Lua 5.3.5, it returns : true      function: 6455a2c0      true    nil
>
> I do not understand what is going on under Lua 5.3.5. Note that, with a
> previous version (Lua 5.3.2 for example), I still have the same result than
> Lua5.2 and 5.1 case.
>
> Similarly, still with Lua 5.3.5, the call pcall(pairs,function() end))
> returns :
> true    function: 6455a2c0      function: 008f7fb0      nil
>
> whereas it would have been returning false for previous versions of Lua.
>
> Can anyone help me understand ?

Is it less puzzling if you don't use pcall?

$ lua5.2
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> for k,v in pairs(1) do print(k,v) end
stdin:1: bad argument #1 to 'pairs' (table expected, got number)
stack traceback:
    [C]: in function 'pairs'
    stdin:1: in main chunk
    [C]: in ?

$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> for k,v in pairs(1) do print(k,v) end
stdin:1: bad argument #1 to 'for iterator' (table expected, got number)
stack traceback:
    [C]: in function 'next'
    stdin:1: in main chunk
    [C]: in ?
>

The error occurs not when calling 'pairs', but when calling the
iterator that it returns.

Thus the new behaviour makes

for k,v in pairs(1) do

and

for k,v in next.1 do

behave the same way (except, of course, if you debug.setmetamethod
__pairs for numbers).