Lua 5.1 varargs with nil question

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua 5.1 varargs with nil question

Norman Ramsey-2
I have some code that uses Lua 5.0 and has varargs functions
in which some arguments may be nil.  Here is an example using Lua 5.0:

  local output
    --- function to output zero or more items, where an item is
    ---    * a string
    ---    * nil (ignored)
    ---    * a function returning zero or more items
    ---    * a list of items in a Lua table
  do
    local output_funs = { }
    output_funs['string']   = output_one
    output_funs['nil']      = function() end
    output_funs['table']    = function(t) return output(unpack(t)) end
    output_funs['function'] = function(f) return output(f()) end
    function output(...)
      for i = 1, table.getn(arg) do --- can't use ipairs b/c arg might be nil
        local v = arg[i]
        assert(output_funs[type(v)], "Bad value to output()") (v)
      end
    end
  end

I am struggling to figure out how to implement this function in Lua 5.1.
How am I to make sure that I get all the arguments when the new #
operator is not specified when some arguments are nil?  
Must I write C code to capture the number of arguments?

  int argc(lua_State *L) {
    lua_pushnumber(L, lua_gettop(L));
    return 1;
  }

Depressing if true...


Norman

Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 varargs with nil question

Adrian Sietsma
Norman Ramsey wrote:

> I have some code that uses Lua 5.0 and has varargs functions
> in which some arguments may be nil.  Here is an example using Lua 5.0:
>
>   local output
>     --- function to output zero or more items, where an item is
>     ---    * a string
>     ---    * nil (ignored)
>     ---    * a function returning zero or more items
>     ---    * a list of items in a Lua table
>   do
>     local output_funs = { }
>     output_funs['string']   = output_one
>     output_funs['nil']      = function() end
>     output_funs['table']    = function(t) return output(unpack(t)) end
>     output_funs['function'] = function(f) return output(f()) end
>     function output(...)
>       for i = 1, table.getn(arg) do --- can't use ipairs b/c arg might be nil
>         local v = arg[i]
>         assert(output_funs[type(v)], "Bad value to output()") (v)
>       end
>     end
>   end
>
> I am struggling to figure out how to implement this function in Lua 5.1.
> How am I to make sure that I get all the arguments when the new #
> operator is not specified when some arguments are nil?  
AFAIK, #... will return the total number of items in the ... object,
including nils.
The problems with nil args only apply to tables as arrays.


(untested)
function output(...)
    for i = 1, select('#',...) do
       local v = select(i,...)
       assert(output_funs[type(v)], "Bad value to output()") (v)
    end
end

Adrian
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 varargs with nil question

Greg Falcon
In reply to this post by Norman Ramsey-2
On 4/23/06, Norman Ramsey <[hidden email]> wrote:

>     function output(...)
>       for i = 1, table.getn(arg) do --- can't use ipairs b/c arg might be nil
>         local v = arg[i]
>         assert(output_funs[type(v)], "Bad value to output()") (v)
>       end
>     end
>
> I am struggling to figure out how to implement this function in Lua 5.1.
> How am I to make sure that I get all the arguments when the new #
> operator is not specified when some arguments are nil?

The standard library function "select" is quite useful when dealing
with the ... expression.  For example:

 function output(...)
   for i = 1, select('#', ...) do
     local v = select(i, ...)
     assert(output_funs[type(v)], "Bad value to output()") (v)
   end
 end

Greg F
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 varargs with nil question

Pavol Severa
In reply to this post by Norman Ramsey-2
how about just including the line
arg={...}
after function output(...)?
P.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 varargs with nil question

Norman Ramsey-2
In reply to this post by Greg Falcon
 > On 4/23/06, Norman Ramsey <[hidden email]> wrote:
 > >     function output(...)
 > >       for i = 1, table.getn(arg) do --- can't use ipairs b/c arg might be
 > >       nil
 > >         local v = arg[i]
 > >         assert(output_funs[type(v)], "Bad value to output()") (v)
 > >       end
 > >     end
 > >
 > > I am struggling to figure out how to implement this function in Lua 5.1.
 > > How am I to make sure that I get all the arguments when the new #
 > > operator is not specified when some arguments are nil?
 >
 > The standard library function "select" is quite useful when dealing
 > with the ... expression.  

Aha!  I somehow missed that one.  It seems the 5.1 manual gives
'incompatibilities' with 5.0 but not 'additions'.  Anyway, 'select' is
perfect; thanks.


N
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 varargs with nil question

Adrian Sietsma
In reply to this post by Greg Falcon
at 2.48 (local time) I wrote:
function output(...)
    for i = 1, select('#',...) do
       local v = select(i,...)
       assert(output_funs[type(v)], "Bad value to output()") (v)
    end
end

at 2.49 Greg Falcon wrote:
>  function output(...)
>    for i = 1, select('#', ...) do
>      local v = select(i, ...)
>      assert(output_funs[type(v)], "Bad value to output()") (v)
>    end
>  end

given mail transit times we must sent almost simultaneously - does lua now
suport quantum entanglement ?

Adrian