lua-users home
lua-l archive

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


On Friday 25 February 2011 15:22:25 Hisham wrote:
> Hi list,
> 
> I bumped into a behavior that surprised me today. I have a function
> that may or may not return a value, and insert that value into a
> table. Given that Lua ignores extra arguments and fills missing
> arguments with nil, I expected that I could just do
> 
>    table.insert(my_table, my_function())
> 
> But I got "wrong number of arguments to 'insert'". What surprised me
> the most, however, was that while passing a nil variable is allowed,
> passing a nullary function is frowned upon:
> 
> Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> 
> > t = {}
> > x = nil
> > table.insert(t, x)
> > f = function() end
> > table.insert(t, f())
> 
> stdin:1: wrong number of arguments to 'insert'
> stack traceback:
>         [C]: in function 'insert'
>         stdin:1: in main chunk
>         [C]: ?
> 

Workaround:

#!/usr/bin/lua
t = {}
x = nil
table.insert(t, x)
f = function() end
local return_holder = f()   -- OK, I'll play your silly game!
table.insert(t, return_holder)

slitt@mydesk:~/junk/vim73$ ./test.lua
slitt@mydesk:~/junk/vim73$

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt